Results 1 to 4 of 4

Thread: Notifying changes in the column collection in custom objects

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Posts
    23

    Question Notifying changes in the column collection in custom objects

    Hello

    Suppose that I have a class that implements the IComponent, IBindingList, ITypedList and the INotifyChange interfaces and then I databound an object of this class to a DataGridView control. I would like to know if there is a way to notify the client that the available "column" collection has changed.

    Please notice that I am not talking about notifying that the rows has change, but that the "schema" has changed. The number of properties, or the datatype of them.

    For example, if initially at runtime the control displays 3 columns and then the underlying object adds a new available property, the DataGrid would still display 3 columns unless somehow the control is notified to repopulate its column collection.

    Although rebinding would do the trick, I wonder if there is a mecanism of notification for this cases.

    Thanks a lot.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Notifying changes in the column collection in custom objects

    When you raise the IBindingList.ListChanged event you specify a ListChangedEventArgs object, which has a ListChangedType property. You can set that property to one of the PropertyDescriptorX values. I haven't tested it but I think that that should prompt a DataGridView to regenerate its columns if its AutoGenerateColumns property is set to True. Note that if you call ResetBindings on a BindingSource and pass True as an argument, that BindingSource raises a ListChanged event with PropertyDescriptorChanged as the ListChangedType value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Posts
    23

    Question Re: Notifying changes in the column collection in custom objects

    Thanks. I am using a class derived from BindingList and I've got it partially working by calling MyBase.OnListChanged(). When adding a new column it works fine, but when calling that method when a column is removed, it raises the ArgumentException stating "Cannot bind to the property or column Field1 on the DataSource" -where Field1 is the column that has just been removed.

    I would appreciate further suggestions.

    Thanks a lot.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Notifying changes in the column collection in custom objects

    I'm not going to provide further suggestions when I don;t even know exactly how you've implemented the last one. This is where you provide the code you've written so I can see what you've done, whether there are any obvious issues with and whether I can reproduce your problem by running it myself. It's a waste of time my looking further afield for a solution if you already have one but you just haven't implemented it properly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width