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


Comments

Author: James20 Jul 2010 Member Level: Bronze   Points : 2

Many thanks for this.
I'm just starting to scope out my project, so this is at the perfect level.
Just read your bio too. I'm certain that I will get to love WPF too. Right now there seems a considerable amount to learn. However, it's all been well thought out, and I think I'll enjoy making that investment.

Guest Author: Philip Sköld16 Feb 2012

This was really helpful! A simple solution, almost impossible to find. Thank you!

Guest Author: user19 May 2012

works like a charm! Thank you!

Guest Author: John14 Jun 2012

I found this solution - C# Read Excel and Show in WPF DataGrid:
http://www.codearsenal.net/2012/06/c-sharp-read-excel-and-show-in-wpf.html

Guest Author: vema08 Aug 2012

IS it possible to use DataSet in WPF pages like ABC.XAML.cs file
Why your telling like this .

Guest Author: vema08 Aug 2012

IS it possible to use DataSet in WPF pages like ABC.XAML.cs file
Why your telling like this .