Resources » Code Snippets » C# Syntax

WPF - Binding DataTable to a WPF Datagrid


Last Updated:   Category: C# Syntax    
Author: Member Level: Gold    Points: 20



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


Did you like this resource? Share it with your friends and show your love!

Responses to "WPF - Binding DataTable to a WPF Datagrid"
Author: James    20 Jul 2010Member 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öld     16 Feb 2012
This was really helpful! A simple solution, almost impossible to find. Thank you!


Guest Author: user     19 May 2012
works like a charm! Thank you!


Guest Author: John     14 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: vema     08 Aug 2012
IS it possible to use DataSet in WPF pages like ABC.XAML.cs file
Why your telling like this .



Guest Author: vema     08 Aug 2012
IS it possible to use DataSet in WPF pages like ABC.XAML.cs file
Why your telling like this .



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India