C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

WPF - Binding DataTable to a WPF Datagrid


Posted Date: 16 Oct 2009    Resource Type: Code Snippets    Category: C# Syntax
Author: ABitSmartMember Level: Diamond    
Rating: 1 out of 5Points: 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.

For more details, visit http://abitsmart.com/?p=234



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
WPF Datagrid binding to a DataTable  .  WPF Binding a Datagrid to a DataTable  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource:

How to handle the cell edit event in the DataGridView

Previous Resource: Resize your image through C# Code
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use