5
dataGridView.Rows.Add(
    metaData.Offset.ToString("X2"),
    metaData.Length,
    metaData.Format,        // This parameter goes to a ComboBox cell which throws an
    metaData.Description,   //     exception above                       
    null,
    null);

What is the valid way to programmatically assign data to DataGridViewComboBoxCell?

Dmitrii Lobanov
  • 4,897
  • 1
  • 33
  • 50
LEMUEL ADANE
  • 8,336
  • 16
  • 58
  • 72
  • The entire snippet is a statement, and hence one line. – leppie Jan 21 '11 at 05:08
  • 1
    if you need to give value to a DataGridViewComboBoxColumn check http://stackoverflow.com/questions/4744384/how-to-set-value-in-datagridviewcomboboxcolumn-from-a-datatable/4744550#4744550 – Binil Jan 21 '11 at 06:49

4 Answers4

16

To Solve this problem, just add “DataError” for DataGridView. That’s all, steps: Double click the datagridview and select the “dataerror” event from the event list.

The DataError event enables you to handle exceptions thrown in code that is called by the control during data processing operations.

thats it :)

Community
  • 1
  • 1
suresh
  • 177
  • 1
  • 3
  • 3
    Just curious - but is it not bad practice to be capturing the error and doing nothing with it? I'm struggling with this issue currently and I can't figure out if I could stop the error being thrown in the first place. – Jen Sep 18 '13 at 01:16
  • if you add dataError . then it silently fails To commit edited cell. – bh_earth0 Aug 03 '17 at 12:00
7

To Solve this problem, add event of "DataError" for DataGridView and then

just write this: e.Cancel = true;

e.g:

private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    e.Cancel = true;
}        
Zolfaghari
  • 1,259
  • 1
  • 15
  • 14
4

A little late to the party, but I think this can still be usefull to others. In my case I got this error because I added a combobox based on an enum like this:

// Combo
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.DataSource = Enum.GetValues(typeof(PhoneNumberType));
combo.DataPropertyName = "PhoneNumberType";
combo.Name = "PhoneNumberType";
phoneNumberGrid.Columns.Add(combo);

Now when the DataGridView created a new (empty) row, this empty value couldn't be mapped to the combo box. So instead of ignoring the error, I set the default value for the combo box. I added the event DefaultValuesNeeded and there just set the value to one of the items from the enum. Like this:

private void phoneNumberGrid_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
        // Prevent System.ArgumentException: DataGridViewComboBoxCell value is not valid
        e.Row.Cells["PhoneNumberType"].Value = PhoneNumberType.Private;
}

.. and the exception went away.

rolandow
  • 1,049
  • 1
  • 15
  • 28
0

I had a similar issue, where I populated my DataSource with a Dictionary<int,string> myDict, where I wanted to display the string but have an integer as an underlying value to use:

comboboxcolumn.DataSource = new BindingSource(myDict, null);
comboboxcolumn.ValueMember = "Key";
comboboxcolumn.DisplayMember = "Value";

what caused my trouble was that I correctly set: comboboxcolumn.ValueType = typeof(int); but in the CellValueNeeded() Method, I then set e.Value = someValueOfMyDict; which is a string. Use the int key instead and you eliminate the error instead of catching it and leaving it unhandled.

sterlingberger
  • 63
  • 1
  • 10