简体   繁体   中英

Object reference not set to an instance of an object in InitializeEditingControlValue of a DataGridView

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.

PS If someone can explain we why I have this problem...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM