2

I know this may be a duplicate, but I have not been able to find a solution that works. I have a datagridview that has a combo box column. When I don't try to set the value of the combo box column, all is fine (i.e., the combo box column is populating). When I try to set the value, I get the infamous "DataGridViewComboBoxCell value is not valid" error. Here is my code:

//Retrieve Data
System.Data.DataRowCollection DR1 = GetData(constants.SQL_1);
System.Data.DataRowCollection DR2 = GetData(constants.SQL_2);

//Populate list for combo box column
List<string> list2 = new List<string>();
foreach (System.Data.DataRow DR in DR2)
    list2 .Add(DR["fieldname"].ToString().Trim());

//Set datasource of combo box column
DataGridViewComboBoxColumn cmb =   (DataGridViewComboBoxColumn)dgv.Columns["comboboxcolumnname"];
cmb.ValueType = typeof(string);
cmb.DataSource = list2 ;

//Populate Data Grid View
foreach (System.Data.DataRow DR in DR1)
{

    DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
    cell.Value = DR["fieldname"].ToString();

    DataGridViewRow row = new DataGridViewRow();

    row.Cells.Add(cell);
    dgv.Rows.Add(row);

 }

How do I set the value of the combo box??

1 Answers1

0

In this Post, it suggested to not set the datasource property of the combobox column at all. So here is what I did:

System.Data.DataRowCollection DRC1 = dictionaries.GetData(constants.SQL_1);
System.Data.DataRowCollection DRC2 = dictionaries.GetData(constants.SQL_2);

foreach (System.Data.DataRow DR1 in DRC1)
{
     DataGridViewComboBoxCell cmb = (DataGridViewComboBoxCell)dgv[0, dgv.Rows.Count - 1];

     foreach (System.Data.DataRow DR2 in DRC2)
     {
         cmb.Items.Add(DRC2["fieldname2"].ToString().Trim());
     }

     cmb.Value = DRC1["fieldname1"].ToString();
}
Community
  • 1
  • 1