Add item to DataGridViewComboBoxColumn

Dennys picture Dennys · Apr 2, 2013 · Viewed 25.4k times · Source

I have a DataGridView and I change one column to a DataGridViewComboBoxColumn. And I can edit the Items to put some data in it and it works.

But I want to put the Items in program, therefore, I try to use dataGridView1.Columns["drop_column"]. But it doesn't have a Items

I found this article but it's to create a new column but I already have a column Add items to DataGridViewComboBoxColumn in DataGridView during runtime.

Update: thanks to Ginosaji and Dona, I add a cast and it works now.

((DataGridViewComboBoxColumn)dataGridView1.Columns["drop_column"]).Items.Add("1");
((DataGridViewComboBoxColumn)dataGridView1.Columns["drop_column"]).Items.Add("2");
((DataGridViewComboBoxColumn)dataGridView1.Columns["drop_column"]).Items.Add("3");

Answer

Doan Cuong picture Doan Cuong · Apr 2, 2013

Here is an example:

DataGridViewComboBoxCell CBCell = new DataGridViewComboBoxCell();

CBCell = DataGridView1.Rows(0).Cells("Your column name");
CBCell.Items.Add("Your item here");