none
DataGridView DefaultValuesNeeded - Row.DataBoundItem Coming up Null

    Question

  • Hi,

    I'm binding a DataGridView to a BindingsSource, which in turn I'm binding to a List of custom Invoice objects.

    The BindingsSource has AllowNew set to True, so when the application opens, the Grid will display a blank row with a * next to it. 

    The Invoice object does have a default constructor (at least a parameterless constructor - that's the same thing, right?), and that constructor is being called before the DataGridView's DefaultValuesNeeded event fires.  I know, becaused I debugged it.  At the time DefaultValuesNeeded fires, there is one Invoice object in the list of Invoices that the grid is bound to.  Yet, for an unknown reason, when I try to look at e.Row.DataBoundItem, it comes up null. 

    I know I could just use MyBindingsSource.Current instead, but I really want to know why the row's DataBoundItem is null.

    Thanks in advance.

    Aaron
    Wednesday, March 10, 2010 6:06 PM

Answers

  • Hi Aaron Edwards,

    The root cause is e.Row is a new row in the DataGridView. The DataBoundItem will not return the related objec if the row is new.
    Below is the code of the DataBoundItem via reflector:
    public object get_DataBoundItem()
    {
        if (((base.DataGridView != null) && (base.DataGridView.DataConnection != null)) && ((base.Index > -1) && (base.Index != base.DataGridView.NewRowIndex)))
        {
            return base.DataGridView.DataConnection.CurrencyManager[base.Index];
        }
        return null;
    }
    
     
    


    Let me know if this does not help.
    Aland Li
    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Monday, March 15, 2010 9:10 AM
    Moderator