I have been searching to hours for something very simple: bind a WPF datagrid to a datatable in order to see the columns at design-time. I can’t get any of the examples to w
Using the Microsoft DataGrid in the following way works for me:
In XAML I include the DataGrid:
<WpfToolkit:DataGrid
Grid.Row="4" Grid.Column="0"
ItemsSource="{Binding Path=GridData, Mode=OneWay}" >
</WpfToolkit:DataGrid>
In my view model I expose a DataView:
public DataView GridData
{
get
{
DataSet ds = new DataSet("MyDataSet");
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM HumanResources.Employee";
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
return ds.Tables[0].DefaultView;
}
}