I have a DataGridView with a DataGridViewComboBoxColumn in the first column that is bind whith a List<MyClass> and a DisplayMember="name" (I dont set the valeumember because i want to get myclass when I accesse to value). The second column is a DataGridViewComboBoxColumn too. It will be bound to some List<String> based on the first DataGridViewComboBoxColumn selectedValue.
All this work just fine, the problem starts when I change the first DataGridViewComboBoxColumn.DataSource. When I add a new Myclass to the datasource it works, but if I select the class I will get an error:
System.ArgumentException: DataGridViewComboBoxCell value is not valid
Code:
//Set columns to Datagridview
DataGridViewImageColumn btnEdit = new DataGridViewImageColumn();
Image gear = (System.Drawing.Image)Properties.Resources.gear;
btnEdit.Image = gear;
datagridview.Columns.Add(btnEdit);
//Table
DataGridViewComboBoxColumn cbTable = new DataGridViewComboBoxColumn();
cbTable.HeaderText = "Table";
cbTable.Name = "Table";
cbTable.DisplayMember = "NameToShow";
//tableDataSource = cbTable.DataSource;
//cbTable.DataSource = moduleClone.SqlQuery.Tables;
datagridview.Columns.Add(cbTable);
//...
//...
//...
//Load data
((DataGridViewComboBoxColumn)datagridview.Columns[1]).DataSource = (List<Table>)moduleClone.SqlQuery.Tables;
//Work just fine
//...
//...
//...
//Now if i reload de data
datagridview.Rows.Clear();
((DataGridViewComboBoxColumn)datagridview.Columns[1]).DataSource = (List<Table>)moduleClone.SqlQuery.Tables;
//This give me one message box with the exception several times when drawing the gridview.
Ok, I found a solution that works for me.
Instead use
DataGridViewComboBoxColumn.DataSource, I directly changeDataGridViewComboBoxColumn.Items. Has a probleme because you cant changeDataGridViewComboBoxColumn.ItemsifDataGridViewComboBoxColumn.DataSourceis set. So I defineDataGridViewComboBoxColumn.DataSource = nullbefore editDataGridViewComboBoxColumn.Itens.Code: