0
votes

I asked this same question last week and ended up getting a solution to my problem thanks to user @Aaron . However, I'm asking again because the code works perfectly in one project but doesn't work in another project under almost the exact same conditions (ie. # of columns/rows, type of variable, how the DGV is populated).

//This is my code to go through each cell in the DataGridView.
for (int i = 0; i < dgvTest.RowCount; i++)
        {
            for (int j = 0; j < dgvTest.ColumnCount; j++)
            {
                foreach (Information info in frmMain._dbList)
                {
                    if (dgvTest.Rows[i].Cells[j].Value.ToString().ToLower() == info.InfoName.ToLower() && info.InfoInputType == "1")
                    {
                        DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
                        c.Items.Add("0");
                        c.Items.Add("1");
                        dgvTest.Rows[i].Cells[(j + 1)] = c;

                    }
                }
            }
        }

PROBLEM:

Error Message

Once I click "OK" oddly enough, it creates the ComboBox. If I repeat this process, it will eventually populate each cell with the ComboBox, but whenever I mouse over them the same error message pops up.

Is it setting the cell to the combobox and then trying to go back over the same cell?

SOLVED

Simple solution - had to add a c.Value = # to set the value.

2

2 Answers

0
votes

To avoid above problem. You could add DataError event for your DataGridView.

Try below code:

private void dgvTest_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    try
    {
        if (e.Exception.Message.contains("DataGridViewComboBoxCell value is not valid."))
        {
            object value = dgvTest.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
            if (!((DataGridViewComboBoxColumn)dgvTest.Columns[e.ColumnIndex]).Items.Contains(value))
            {
                ((DataGridViewComboBoxColumn)dgvTest.Columns[e.ColumnIndex]).Items.Add(value);
            }
        }

        throw e.Exception;
    }
    catch (Exception ex)
    {
        MessageBox.Show(string.Format(@"Failed to bind ComboBox. "
            + "Please contact support team with below error message:"
            + "\n\n" + ex.Message));
    }
}

Above, will throw error message to the user. If you want to suppress this error. You just follow the below code:

private void dgvTest_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    try
    {
        if (e.Exception.Message.contains( "DataGridViewComboBoxCell value is not valid."))
        {
            object value = dgvTest.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
            if (!((DataGridViewComboBoxColumn)dgvTest.Columns[e.ColumnIndex]).Items.Contains(value))
            {
                ((DataGridViewComboBoxColumn)dgvTest.Columns[e.ColumnIndex]).Items.Add(value);
            }
        }
    }
    catch (Exception ex)
    {
        //do nothing
    }
}
0
votes

Had to set the initial value of the ComboBox to something.

c.Value = #