I want to create a datagridview with comboboxcolumns using c#.
The problem is that I dont know how to give different values for combobox in each row.
DataTable dt = new DataTable();
dt.Columns.Add("state");
dt.Columns.Add("city");
dt.Rows.Add("a1", "b1");
dt.Rows.Add("a1", "b2");
dt.Rows.Add("a2", "b3");
dt.Rows.Add("a2", "b4");
dt.Rows.Add("a3", "b5");
DataGridViewComboBoxColumn comboStates = new DataGridViewComboBoxColumn();
comboStates.HeaderText = "HeaderText_1";
this.dataGridView1.Columns.Insert(0, comboStates);
DataGridViewComboBoxColumn comboCities = new DataGridViewComboBoxColumn();
comboCities.HeaderText = "HeaderText_2";
this.dataGridView1.Columns.Insert(1, comboCities);
for (int i = 0; i < dt.Rows.Count; i++)
{
dataGridView1.Rows.Add();
comboStates.Items.Add(dt.Rows[i][0]);
DataGridViewComboBoxCell stateCell = (DataGridViewComboBoxCell) (dataGridView1.Rows[i].Cells[0]);
stateCell.Value = comboStates.Items[i];
comboCities.Items.Add(dt.Rows[i][1]);
DataGridViewComboBoxCell cityCell = (DataGridViewComboBoxCell)(dataGridView1.Rows[i].Cells[1]);
cityCell.Value = comboCities.Items[i];
}
This example gives the following results :
for each row:
comboboxcolumnstate :
a1
a1
a2
a2
a3
comboboxcolumncity :
b1
b2
b3
b4
b5
I know this is normal because I am looping through datatable.
So how can i get this result:
row1: comboboxcolumnstate comboboxcolumncity
a1 b1 - b2
row2: comboboxcolumnstate comboboxcolumncity
a2 b3 - b4
row2: comboboxcolumnstate comboboxcolumncity
a3 b5
I am new to C#. I searched a lot but I didn't find a solution which solve this issue. Thanks
Here is an example code that should get you started.
First I create a list of values (cities) for each state. For this the
Dictionary
collection comes handy:I loop over your table and for each row I add a new entry for the city and, if necessary, for the state.
Now we modify the setup of the DGV to use those
Lists
in theItems
of each of theDataGridViewComboBoxCell
.Note: Here the key is to use the
DataGridViewComboBoxCells
, and not theDataGridViewComboBoxColumns
!For easier access I create string variables for the state and city values again..
Note: This sets up the values in the DropDowns, but after each change in a 'State' column you will need to adapt the 'Cities' column in that row! Therefore it would be a good idea to make the
Dictionary
persistent by moving it to class level and to create a little function you can call after such a change.Here is an example of this functionality:
Note that the new
cityCell.Value
shows up only after leaving the edited cell!Final note: If you really want to display only one row per state, you need to change the loop that fills the
DGV
to loop not over your table but over thedict.Keys
collection: