WPF DataGrid to sql DataTable Binding problem

May 14, 2010 at 5:34 PM
Edited May 14, 2010 at 5:39 PM

Hello All, I'm new and trying to learn WPF.  What I'm trying to do is fairly simple and probably being a newbie, this question may sound dumb.  But I did search both here and google, still no answer.  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


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.


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:


DataAdapter.SelectCommand.CommandText="select * from Customer" --> where customer is another table in the database


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:


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 thing 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 17, 2010 at 4:33 PM

This would NOT be a DataGrid issue.  The problem should be in the DataTable object.  If you want to test, simply declare another DataTable Object and switch back and forth using the same Grid, I'm sure you going to see is not the grid problem.  Now, as far as re-using the same DataTable, I would suggest that you simply create another DataTable and dispose the old one.  I simply don't see any advantage of using the same DataTAble object unless you have some special reason which you can tell me and I can see how to attack the problem.