|
-
Jun 6th, 2010, 06:21 AM
#1
Thread Starter
New Member
[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?
-
Jun 6th, 2010, 06:25 AM
#2
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.
-
Jun 6th, 2010, 06:35 AM
#3
Thread Starter
New Member
Re: BindingSource not updating
 Originally Posted by jmcilhinney
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?
-
Jun 6th, 2010, 07:17 AM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|