Windows Forms DataGridView controls have different types of controls in the same column

Is it possible to do the following in a DataGridView:

In the same column, I want to change the control type of each row between a DataGridViewTextBoxColumn and a DataGridViewComboBoxColumn?

(this is because sometimes I want to display a drop-down list, and sometimes I want the user to enter a freehand value).

Thanks,

PS I am using C #

+7
c # winforms datagridview
source share
3 answers

You can create your own class inherited from DataGridViewCell and override the corresponding virtual members (InitializeEditingControls, EditType, possibly several others). Then you can create a DataGridViewColumn with an instance of this class as a cell template

0
source share

I recently had a similar utility, and in the end I wrote something like the code below:

Write your own class of cells and columns and redefine the EditType and InitializeEditingControl control methods in the cell to return the various controls accordingly (here I just bind the data to the list of the user class with a .useCombo field indicating which control to use):

// Define a column that will create an appropriate type of edit control as needed. public class OptionalDropdownColumn : DataGridViewColumn { public OptionalDropdownColumn() : base(new PropertyCell()) { } public override DataGridViewCell CellTemplate { get { return base.CellTemplate; } set { // Ensure that the cell used for the template is a PropertyCell. if (value != null && !value.GetType().IsAssignableFrom(typeof(PropertyCell))) { throw new InvalidCastException("Must be a PropertyCell"); } base.CellTemplate = value; } } } // And the corresponding Cell type public class OptionalDropdownCell : DataGridViewTextBoxCell { public OptionalDropdownCell() : base() { } public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) { // Set the value of the editing control to the current cell value. base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle); DataItem dataItem = (DataItem) this.OwningRow.DataBoundItem; if (dataItem.useCombo) { DataGridViewComboBoxEditingControl ctl = (DataGridViewComboBoxEditingControl)DataGridView.EditingControl; ctl.DataSource = dataItem.allowedItems; ctl.DropDownStyle = ComboBoxStyle.DropDownList; } else { DataGridViewTextBoxEditingControl ctl = (DataGridViewTextBoxEditingControl)DataGridView.EditingControl; ctl.Text = this.Value.ToString(); } } public override Type EditType { get { DataItem dataItem = (DataItem)this.OwningRow.DataBoundItem; if (dataItem.useCombo) { return typeof(DataGridViewComboBoxEditingControl); } else { return typeof(DataGridViewTextBoxEditingControl); } } } } 

Then just add a column to your DataGridView of this type, and you need to use the correct edit control.

+7
source share

You can create a template column with two controls, hide the one you don’t want, and associate the other with the data source in the code.

0
source share

All Articles