Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datagridview combobox cell c#

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

like image 400
santBart Avatar asked Oct 24 '25 04:10

santBart


1 Answers

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.

like image 75
David Hall Avatar answered Oct 25 '25 17:10

David Hall