protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e) { base.OnEditingControlShowing(e); try { if (e.Control is DataGridViewComboBoxEditingControl) { ((ComboBox)e.Control).IntegralHeight = false; ((ComboBox)e.Control).MaxDropDownItems = 10; ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown; ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems; ((ComboBox)e.Control).AutoCompleteMode = AutoCompleteMode.Suggest; } else if (e.Control is DataGridViewTextBoxEditingControl) { DataGridViewColumn c = this.Columns[this.CurrentCell.ColumnIndex]; grid_cell_type ct = c.Tag != null && c.Tag is grid_cell_type ? (grid_cell_type)c.Tag : grid_cell_type.none; if (ct == grid_cell_type.date) { object o = this.CurrentCell.Value; if (o != null && o is DateTime) { ((TextBox)e.Control).Text = ((DateTime)o).ToString("dd/MM/yyyy"); if (((TextBox)e.Control).Tag == null) { ((TextBox)e.Control).KeyDown += date_dgv_KeyDown; ((TextBox)e.Control).Tag = "init"; } } } } } catch { } }
protected override void OnCellMouseEnter(DataGridViewCellEventArgs e) { base.OnCellMouseEnter(e); try { if (e.RowIndex < 0 || e.ColumnIndex < 0) { return; } DataGridViewColumn c = this.Columns[e.ColumnIndex]; grid_cell_type ct = c.Tag != null && c.Tag is grid_cell_type ? (grid_cell_type)c.Tag : grid_cell_type.none; if (ct == grid_cell_type.image) { this.Cursor = Cursors.Hand; } else { this.Cursor = Cursors.Default; } } catch { } }
protected override void OnKeyDown(KeyEventArgs e) { try { if (e.Control && (e.KeyCode == Keys.Add || e.KeyCode == Keys.Subtract)) { DataGridViewColumn c = this.Columns[this.CurrentCell.ColumnIndex]; if (!c.ReadOnly) { grid_cell_type ct = c.Tag != null && c.Tag is grid_cell_type ? (grid_cell_type)c.Tag : grid_cell_type.none; if (ct == grid_cell_type.date) { this.BeginEdit(true); DateTime dt = Convert.ToDateTime(this.CurrentCell.Value); this.CurrentCell.Value = dt.AddDays(e.KeyCode == Keys.Add ? 1 : -1); this.EndEdit(); } } } else { base.OnKeyDown(e); } } catch { } }
public int add_col_dgv(string field, string title, grid_cell_type cell_type = grid_cell_type.text , int width = 100, bool wrap = false, string after_col = "", bool fill = false, string format = "" , int maxlength = 0, bool?read_only = null, bool bold = false, Color?fc = null) { DataGridViewColumn add_col = null; if (cell_type == grid_cell_type.text) { add_col = new DataGridViewTextBoxColumn() { HeaderText = title } } ; else if (cell_type == grid_cell_type.check) { add_col = new DataGridViewCheckBoxColumn() { AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells, HeaderText = title }; add_col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } else if (cell_type == grid_cell_type.link) { add_col = new DataGridViewLinkColumn() { HeaderText = title, ActiveLinkColor = Color.White, LinkBehavior = LinkBehavior.SystemDefault , LinkColor = Color.Blue, TrackVisitedState = true, VisitedLinkColor = Color.DodgerBlue } } ; else if (cell_type == grid_cell_type.combo) { add_col = new DataGridViewComboBoxColumn() { HeaderText = title } } ; // aggiunta colonna int nc = -1, i = 0; if (after_col != "") { foreach (DataGridViewColumn dgvc in this.Columns) { if (dgvc.Name == after_col) { nc = i + 1; break; } i++; } } int n = -1; if (nc >= 0) { n = nc; if (add_col != null) { this.Columns.Insert(n, add_col); } else { this.Columns.Insert(n, new DataGridViewColumn() { HeaderText = title }); } } else { n = add_col != null?this.Columns.Add(add_col) : this.Columns.Add(field, title); } // init this.Columns[n].Tag = cell_type; this.Columns[n].Name = field; this.Columns[n].DataPropertyName = field; if (read_only.HasValue) { this.Columns[n].ReadOnly = read_only.Value; } if (width >= 0) { this.Columns[n].Width = width; } this.Columns[n].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; if (wrap) { this.Columns[n].DefaultCellStyle.WrapMode = DataGridViewTriState.True; } if (fill) { this.Columns[n].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } if (maxlength > 0 && this.Columns[n] is DataGridViewTextBoxColumn) { ((DataGridViewTextBoxColumn)this.Columns[n]).MaxInputLength = maxlength; } //if(bold && this.style != null) this.Columns[n].DefaultCellStyle.Font = this.style.font_grid_bold; if (fc.HasValue) { this.Columns[n].DefaultCellStyle.ForeColor = fc.Value; } // format if (string.IsNullOrEmpty(format)) { if (cell_type == grid_cell_type.money) { this.Columns[n].DefaultCellStyle.Format = "C"; } else if (cell_type == grid_cell_type.date) { this.Columns[n].DefaultCellStyle.Format = "ddd dd/MM/yy"; } else if (cell_type == grid_cell_type.date_time) { this.Columns[n].DefaultCellStyle.Format = "dd/MM/yy HH:mm:ss"; } } else { this.Columns[n].DefaultCellStyle.Format = format; } // alignment if (cell_type == grid_cell_type.integer || cell_type == grid_cell_type.money) { this.Columns[n].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; } else if (cell_type == grid_cell_type.check || cell_type == grid_cell_type.date || cell_type == grid_cell_type.date_time) { this.Columns[n].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } else { this.Columns[n].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; } // value type this.Columns[n].ValueType = cell_type == grid_cell_type.integer ? typeof(int) : (cell_type == grid_cell_type.date || cell_type == grid_cell_type.date_time ? typeof(DateTime) : (cell_type == grid_cell_type.money ? Type.GetType("System.Decimal") : typeof(string))); return(n); }