Menu

Adding custom business object to DataGridView

Help
2008-03-04
2013-04-18
  • Corey Sullivan

    Corey Sullivan - 2008-03-04

    Hi,

    I have been using this library for sometime now and it has been extremely useful.  Recently, I have made a DataGridView that I can add, edit, and delete objects the default way (in grid CRUD.)  The DataGridView is hooked up to a BindingSource with its DataSource property set to a BindingListView with a BindingList of my custom object.  The problem I am having is that I have a property in my custom business object that uses the value of another property in the object, i.e.:

    public int Amount
    {
        get
        {
            return this._amount;
        }
        set
        {
            if (this._amount!= value)
            {
                this._amount = value;
                this.OnPropertyChanged("Amount");
                this.OnPropertyChanged("Amount10";
            }
        }
    }

    public int Amount10
    {
        get
        {
            return this._amount * 10;
        }
    }

    I data bind the DataGridView through the Windows Forms Designer and set the DataSource of the BindingSource to a BindingListView with the source data source.  I am implementing the INotifyPropertyChanged interface in this class.  While I add a new row to the DataGridView (by entering data on the "new row" in the DataGridView), the Amount10 column doesn't update as I would expect after entering data for the Amount column.  The current data for Amount10 only appears if I focus on the Amount10 cell, focus off the row I added and back to it, or I add another new item through the grid.  When I stepped through the code (using Visual Studio 2008) I found that this call in AggregateBindingListView.ItemPropertyChanged (line 760):

    int index = _sourceIndices.IndexOfItem((T)sender);

    always returns -1 because the sender is the new row and hasn't been committed to the object data source.  The subsequent code:

    if (index > -1)
    {
        // Tell listeners that an item has changed.
        // This is inline with the IRaiseItemChangedEvents implementation.
        OnListChanged(ListChangedType.ItemChanged, index);
    }

    obviously doesn't run and won't update the DataGridView with the correct value for the Amount10 column.

    I apologize that this was long-winded, but it was an issue I would point out.  Again, thank you for a great library that has saved me plenty of time not implementing something like this over and over again.

    Corey Sullivan

     
    • Andrew Davey

      Andrew Davey - 2008-03-10

      Hello,

      Thanks for the detailed analysis! I'm sorry I've been very busy and slow at following up emails recently.

      I'm not able to work on this project right now (too many other commitments). However, if you are able to fix the code and create a patch I'm happy to update the code.

      Regards,
      Andrew

      P.S. Thank you for the donation :D

       

Log in to post a comment.

MongoDB Logo MongoDB