My code:
DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
DataTable data = new DataTable();
data.Columns.Add(new DataColumn("Value", typeof(string)));
data.Columns.Add(new DataColumn("Description", typeof(string)));
data.Rows.Add("5", "6");
data.Rows.Add("51", "26");
data.Rows.Add("531", "63");
cell.DataSource = data;
cell.ValueMember = "Value";
cell.DisplayMember = "Description";
cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
dataGridView1.Rows[0].Cells[0] = cell;
It displays combobox but can't select any value of it. Whats wrong
You generally don't work with the individual cell types in the DataGridView. Instead you want to add a column of type DataGridViewComboBoxColumn.
So instead of your provided code you want something like:
var column = new DataGridViewComboBoxColumn();
DataTable data = new DataTable();
data.Columns.Add(new DataColumn("Value", typeof(string)));
data.Columns.Add(new DataColumn("Description", typeof(string)));
data.Rows.Add("5", "6");
data.Rows.Add("51", "26");
data.Rows.Add("531", "63");
column.DataSource = data;
column.ValueMember = "Value";
column.DisplayMember = "Description";
dataGridView1.Columns.Add(column);
For reference, the documentation on the DataGridViewCombobBoxCell is on MSDN here. You can also find information on the DataGridView in general there. Another very good reference is the DataGridViewFAQ.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With