Results 1 to 3 of 3

Thread: [RESOLVED] [2005] BindingDataSource Add & Remove

  1. #1

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Resolved [RESOLVED] [2005] BindingDataSource Add & Remove

    I have created a DataSource Object that refers to a class I have created, this DataSource is binded to a DataGridView Control to display the contents of the class which works perfectly.

    I am trying to add and remove items from the DataGridView dynamically, but should I do it by removing or adding the item from/to the class or the BindingDataSource?

    VB Code:
    1. Dim Accts As New AccountsCollection()
    2.  
    3. AccountCollectionBindingSource.AddNew(New Account("A","B"))
    4. AccountCollectionBindingSource.RemoveAt(1)
    5.  
    6. ' Or
    7.  
    8. Accts.Add(New Account("A","B"))
    9. Accts.RemoveAt(1)

    Both these methods work to adding or removing an item from the class object but which method is prefered or doesn't it matter?

    The second part of my question is how do I get the DataGridView to reflect the changes?

    VB Code:
    1. ' Now reset the datasource to reflect the changes?
    2. AccountCollectionBindingSource.DataSource = Accts

    The changes never are reflected until I restart the application.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

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

    Re: [2005] BindingDataSource Add & Remove

    If you're using a BindingSource then you should generally work with the BindingSource. I would do that unless there was a specific need to work with the original data source, which in this case there isn't. If you are going to work with the original data source then using the BindingSource at all doesn't really serve a purpose.

    Also, there should be no need to make any manual changes to the grid. The whole point of data-binding is that what affects one affects the other. If you make changes to the data source then the grid should reflect those changes. I've never seen a situation where that didn't happen.

    Finally, the past tense of "bind" is "bound", not "binded".
    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
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: [2005] BindingDataSource Add & Remove

    Finally, the past tense of "bind" is "bound", not "binded".
    Lol doh!

    Thanks for the input thats probably right.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

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