Tanuj Wadhwa
Tanuj Wadhwa

Reputation: 2045

Binding DataSet to DataGrid in WPF

I know this has been asked before several times but I am not able to get this. I have a DataSet and a DataGrid. All I want to do is display the contents of the DataSet in the DataGrid.

I have written this code :

vConn = new OleDbConnection(ConnectionString);
vConn.Open();

vQuery = "Select * from Book";

DataSet vDs = new DataSet();
OleDbDataAdapter vAdap = new OleDbDataAdapter(vQuery, vConn);
vAdap.Fill(vDs,"Book");

GridData.DataContext = vDs.Tables["Book"];
vConn.Close();

But for some reason the data is not shown on DataGrid. I have tried setting AutoGenerateColumn to True/False. I also tried binding in xaml but it didn't work.

Upvotes: 11

Views: 54500

Answers (2)

Colin
Colin

Reputation: 551

OK, so here may be the same issue you are dealing with.

You can try to set the ItemsSource property as follow:

GridData.ItemsSource = vDs.Tables["Book"].AsEnumerable();

Upvotes: 1

dkozl
dkozl

Reputation: 33364

this should work:

GridData.ItemsSource = vDs.Tables["Book"].DefaultView;

or you can create your own DataView:

GridData.ItemsSource = new DataView(vDs.Tables["Book"]);

DataTable.DefaultView gives you DataView which implements IEnumerable and can be used as ItemsSource

Upvotes: 14

Related Questions