Search code examples
c#datagridviewdatagridviewcomboboxdatagridviewcomboboxcell

i want to build datagirdviewcomboboxcell dynamically


I want to build a DataGridView, which if I choose one of the item of a DataGridViewComboBoxCell, then the other cells in the same row which should be TextBoxes in other rows will turn into ComboBoxes,does anyone knows how to do that?

It is like:

TextBox1|TextBox2|ComboBox1.Item1|TextBox3 |TextBox4 |TextBox5 |TextBox6
TextBox1|TextBox2|ComboBox1.Item3|TextBox3 |TextBox4 |TextBox5 |TextBox6
TextBox1|TextBox2|ComboBox1.Item2|ComboBox3|ComboBox4|ComboBox5|ComboBox6

Solution

  • If you want to do something when the Value in a DataGridViewCell changes then you should handle the CellValueChanged event of the grid.

    If you want to place a cell of a specific type in a specific location in a DataGridView then you can do so using the grid's indexer, e.g.

    myDataGridView[columnIndex, rowIndex] = new DataGridViewComboBoxCell();
    

    In summary, handle the CellValueChanged event, use an if statement to test whether the Value is one that corresponds to a text box or combo box and, if the types of the other cells are not what they should be, replace them.