|
-
May 15th, 2010, 12:20 PM
#1
Thread Starter
New Member
WPF DataGrid to sql DataTable RE-Binding problem
Hello All, I'm new and trying to learn WPF. What I'm trying to do is fairly simple and being a newbie, this question may sound dumb and probably why I couldn't find answer? (I've searched google and other forums including this one). The following is my code (again, fairly simple and straight forward):
Public DataAdapter as New sqlClient.sqlDataAdapter(sqlCommandstr,sqlConnectStr)
Public dbTable as new DataTable
First I define the sqlAdapter and a data table object, now,
DataAdapter.SelectCommand.CommandText="select * from Company" --> where Company is just a table with few fields
DataAdapter.fill(dbTable)
Ok, so now the dbTable object is filled with the data extracted form SQL Server's "Company" table. Now I will fill the "DataGrid1" WPF datagrid control that I simply drag and drop into the WPF window.
DataGrid1.ItemSource=dbTable.DefaultView
Simple enough! And now hit the "run" button and the grid will show all the data from table "Company" on the screen.
Now here the PROBLEM. When I try to populate the same table object "dbTable" with a different set of data from SQL Server by:
dbTable.reset()
DataAdapter.SelectCommand.CommandText="select * from Customer" --> where customer is another table in the database
DataAdapter.fill(dbTable)
Ok Now I've check the dbTable, I'm sure it is with new data in there and the old data of "company" table has been wiped clean.
But when I do:
DataGrid1.ItemsSource=dbTable.DefaultView
The DataGrid1 now show only the header of the old table (the company table) and with ZERO rows. Why is that? Why the Grid did not show the new "Customer" table?
I've tried many things that were suggested throughout the search (on both google and here), but no use.
I've tried to do Datagrid1.columns.clear(), Datagrid1.rows.clear() AND I've tried Datagrid1.itemssource=nothing or DataGrid1.items.Refresh(), NOTHING WORKS. The grid would either show the old header with no data or the grid will simply be wiped empty. So when the underlying data of the table object "dbTable" changes, how can I rebind the Grid to show the new dataset?
P.S. I have the AutoGenerateColumns set the true.
Thank you!
-
May 21st, 2010, 03:18 PM
#2
Re: WPF DataGrid to sql DataTable RE-Binding problem
That does sound odd as it sounds like you are doing everything correctly...
What happens if you set AutoGenerateColumns to false and just create the relevant columns in code manually before resetting the ItemsSource property? Obviously if that works it is not a fix as you should not have to do that - its just to test it to try and find out where the problem is.
Also, thinking about it, WPF does not have a DataGrid control built in - so where have you got this DataGrid control from? EDIT: Oh actually I see they have included it in VS 2010 / .NET 4.0 so I assume you are using that?
-
May 21st, 2010, 03:33 PM
#3
Re: WPF DataGrid to sql DataTable RE-Binding problem
Aha I found the problem 
Instead of calling the Reset method on the DataTable, you need to just dispose it and create a new DataTable. So your code would look like this:
Code:
dbTable.Dispose()
dbTable = New DataTable
DataAdapter.SelectCommand.CommandText = "select * from Customer"
DataAdapter.Fill(dbTable)
DataGrid1.ItemsSource = dbTable.DefaultView
Does that make it work how you want it to now?
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
|