Results 1 to 4 of 4

Thread: [RESOLVED] BindingSource not updating

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    2

    Resolved [RESOLVED] BindingSource not updating

    I'm trying to display a DataTable using a DataGridView and a BindingSource as such: (assuming the DataGridView1 and BindingSource1 has been added using the IDE)
    Code:
    Dim table As New DataTable
    
    '
    'populate table with some data here
    '
    
    BindingSource1.DataSource = table
    DataGridView1.DataSource = BindingSource1
    This works fine most of the time; deleted rows from the DataGridView are reflected back on the table, and added rows using table.Rows.Add() are reflected on the DataGridView as well.
    However, when I try to change the entire table the BindingSource failes to update the DataGridView:
    Code:
    Dim NewTable As New DataTable
    
    '
    'populate NewTable with some data here
    '
    
    'set the table to NewTable
    table = NewTable
    I tried to use BindingSource1.ResetBindings() method but that didn't work; the DataGridView was redrawn but still showed values from the old table. The only thing that did work was this:
    Code:
    BindingSource1.DataSource = Nothing
    BindingSource1.DataSource = table
    However this is not the best solution to my problem since the table is actually being changed by a parent forum. Is there a way to cause the BindingSource to update automatically in this scenario?

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

    Re: BindingSource not updating

    The DataGridView will, by default, automatically create the required columns when you bind data to it. It will never remove columns though. If you want to change the bound table you should:

    1. Set the grid's DataSource to Nothing.
    2. Clear the grid's Columns collection.
    3. Assign the new Datatable to the BindingSource's DataSource.
    4. Assign the BindingSource to the grid's DataSource.
    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
    New Member
    Join Date
    Jun 2010
    Posts
    2

    Re: BindingSource not updating

    Quote Originally Posted by jmcilhinney View Post
    The DataGridView will, by default, automatically create the required columns when you bind data to it. It will never remove columns though. If you want to change the bound table you should:

    1. Set the grid's DataSource to Nothing.
    2. Clear the grid's Columns collection.
    3. Assign the new Datatable to the BindingSource's DataSource.
    4. Assign the BindingSource to the grid's DataSource.
    I forgot to mention this, but I'm not changing columns; table and NewTable both have the same columns that are defined identically. I am merely trying to update (clear and repopulate) the rows in the table. Do I still need to reset the DataSource as you suggested?

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

    Re: BindingSource not updating

    In that case you should just use the same DataTable. Presumably you called Fill on a DataAdapter to populate the DataTable in the first place. Call Clear on the DataTable to get rid of the old data and then repopulate it in exactly the same way you populated it in the first place. In fact, you should just have one method that takes a DataTable and populates it, then you call that method in both cases: to populate and then to repopulate.
    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