-2

I have a DataGridView, and inside a ComboBox cell which content a list of items. When we click on one of those item, I show the content of these "Item" in my main window. BUT if the user click on the small arrow to select an item but finally don't choose any, I got a NullReferenceException (Object reference not set to an instance of an object).

I found something on Google saying that I need to implement my own ComboBoxCell (or column) but the only example I found is even worse that what I got.

For your information, I fill the items of the ComboBoxCell in DataBindingComplete, I put the value in RowPrePaint. Ho yeah: and each ComboBoxCell got a different list of "Items".

Please help found a solution.

P.S. If someone can explain we why I have this problem...

Bestter
  • 877
  • 1
  • 12
  • 29

1 Answers1

0

Because it is likely that you are picking up the selected ComboBox item via an accessor using code something like:

this.textBox1.Text = MyForm.ComboItemValue();

where in the form housing the ComboBox, you will have

public string ComboItemValue
{
    get 
    { 
        if (this.datGridView.CurrentCell.GetType() == typeof(DataGridViewComboBoxCell))
            return this.dataGridView.CurrentCell.Value.ToString();
    }
}

Clearly if the value is left empty this will return a null.

Having said all this, it is incredibly difficult to guess at the problem without you posting any code!

I hope this helps.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277