I'm currently trying to add a ComboBox to a dataGridView.
In the DGV, there are 5 columns: checkbox, string, string, combobox, combobox.
both combobox-columns are configured as datagridviewcomboboxcolumns (via VisualStudio designer). My problem is to add rows.
My current try is: the columns are already defined and I add rows via dataGridView.Rows.Add. For that, I'm using an array of objects. Example:
dataGridViewRow row = new dataGridViewRow();
object[] obj = new object[5] {true, "str1", "str2", null, null};
dataGridView1.Rows.Add(obj);
This passes without any errors. But logically, the comboBoxes aren't filled with anything.
I tried setting a datasource to the 4th and 5th cell of a row:
Error...Using ROW.dataGridViewComboBoxCell.Items.Add: Items are not displayed...
filling obj[3] and 4 with a new DGVcomboBoxCell or -Column:
Error... :The error message says "The dataGridViewComboBoxCell-Value is invalid.
Further information: Each column should have the same Items in the comboBoxes. (These are previously loaded via internet, as xml). Setting a dataSource to the two columns destroys the whole DGV (I think because the other colmns don't have a Datasource). In a nutshell: How to add Rows to a DGV which contain comboboxes filled with items?
Sincerely, NoMad
edit: here's some code to solve my problem:
DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
check.Name = "Col1";
dataGridView1.Columns.Add(check);
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[1].Name = "Col2";
dataGridView1.Columns[2].Name = "Col3";
object[] row = new object[] { true, "str1", "str2" };
dataGridView1.Rows.Add(row);
DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn();
DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();
combo1.Name = "Col4";
combo1.Items.Add("100x100");
combo1.Items.Add("200x200");
combo2.Name = "Col5";
combo2.Items.Add("option1");
combo2.Items.Add("option2");
dataGridView1.Columns.Add(combo1);
dataGridView1.Columns.Add(combo2);
First add a row, cast columns, configure them and add them to the row. No Columns need to be previously specified in the designer.