Question

I am adding a row on the button click event and adding combobox in a row cell but I am not able to bind the values in that cell combobox. Can any one suggest me what I am doing wrong??

DataGridViewRow dgr = new DataGridViewRow();            

DataGridViewComboBoxCell cbCol = new DataGridViewComboBoxCell();
cbCol.Items.Add("Test");
cbCol.Items.Add("Test");
cbCol.Items.Add("Test");
cbCol.Items.Add("Test");       

dgr.Cells.Add(cbCol);

dataGridView1.Rows.Add(dgr);

The above code is written in a button click event.

Was it helpful?

Solution

What you are attempting to do should not be necessary.

You grid should have a column of type DataGridViewComboBoxColumn which has a DataSource set that provides the values available for all cells in that column.

When you add a new row to the grid you will automatically get a cell in the combobox column with the values you need. If you want to have different values in each row then you can use the EditingControlShowing event of the grid to change the combobox data source.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top