WPF - Binding DataTable to a WPF Datagrid
The following code snippet demonstrates different ways to bind a ADO.Net DataTable to a WPF Datagrid.
WPF Datagrid can be found at this location - http://www.codeplex.com/wpf
1. Setting the ItemsSource of the Datagrid to the DataTable's DefaultView
XAML,
<Window x:Class="WpfDataSet.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dtgrd="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
Title="Window1" Height="400" Width="400">
<Grid Name="_maingrid">
<dtgrd:DataGrid
x:Name="_dataGrid"
ColumnHeaderHeight="25"
AutoGenerateColumns="True"
>
</dtgrd:DataGrid>
</Grid>
</Window>
Code-behind,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
namespace WpfDataSet
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private DataSet _ds;
public Window1()
{
InitializeComponent();
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);
DataColumn cl = new DataColumn("Col1", typeof(string));
cl.MaxLength = 100;
dt.Columns.Add(cl);
cl = new DataColumn("Col2", typeof(string));
cl.MaxLength = 100;
dt.Columns.Add(cl);
DataRow rw = dt.NewRow();
dt.Rows.Add(rw);
rw["Col1"] = "Value1";
rw["Col2"] = "Value2";
_datagrid.ItemsSource = ds.Tables[0].DefaultView;
}
}
}
2. Setting the DataContext to the table and then binding ItemsSource to the DataContext
XAML,
<Window x:Class="WpfDataSet.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dtgrd="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
Title="Window1" Height="400" Width="400">
<Grid Name="_maingrid">
<dtgrd:DataGrid
x:Name="_dataGrid"
ItemsSource="{Binding Path=.}"
ColumnHeaderHeight="25"
AutoGenerateColumns="True"
>
</dtgrd:DataGrid>
</Grid>
</Window>
Code-behind,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
namespace WpfDataSet
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private DataSet _ds;
public Window1()
{
InitializeComponent();
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);
DataColumn cl = new DataColumn("Col1", typeof(string));
cl.MaxLength = 100;
dt.Columns.Add(cl);
cl = new DataColumn("Col2", typeof(string));
cl.MaxLength = 100;
dt.Columns.Add(cl);
DataRow rw = dt.NewRow();
dt.Rows.Add(rw);
rw["Col1"] = "Value1";
rw["Col2"] = "Value2";
_datagrid.DataContext = ds.Tables[0];
}
}
}
Have fun.
Reference: http://abitsmart.com/?p=234
This was really helpful! A simple solution, almost impossible to find. Thank you!