Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding DataSet to DataGrid in WPF

Tags:

c#

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.

like image 602
Tanuj Wadhwa Avatar asked Jun 06 '13 14:06

Tanuj Wadhwa


1 Answers

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

like image 65
dkozl Avatar answered Sep 28 '22 23:09

dkozl