Saturday 22 March 2014

How to binding DataGrid in WPF with xml, Sql table and Class Property

How to binding DataGrid in WPF with xml, Sql table and Class Property 

Note:  In this topic we will discuss about how to bind DataGrid in WPF.

First Method…………………………….

1. First we bind this dataGrid with Class and Property then we take a separate class in our application which name is Student.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BindDataGridinWPF
{

class Student
{

public int RollNo { get; set; }
public string Name { get; set; }
public string Gender{ get; set; }
public string Course { get; set;}

}
}

After we create MainWindow.xaml……………………………

Code with MainWindow.xaml ………………………………

<Window x:Class="BindDataGridinWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Background="Azure">

<Grid>
<DataGrid  x:Name="studentdatagrid" HorizontalAlignment="Left" Margin="87,78,0,0" VerticalAlignment="Top" Height="111" Width="275" Background="Beige"/>
</Grid>
</Window>

Code with MainWindow.xaml.cs ……………………………

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace BindDataGridinWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
studentdatagrid.ItemsSource = new List<Student>
{
new Student{RollNo=1,Name="Kush Tiwari",Gender="Male",Course="Mca"},
new Student{RollNo=2,Name="Somesh Katiyar",Gender="Male",Course="B.Tech"},
new Student{RollNo=3,Name="Udayan Maiti",Gender="Male",Course="Mca"},
new Student{RollNo=4,Name="Ravinder Goel",Gender="Male",Course="M.Tech"}
};

}
}
}

Output:

Second Method…………………………….

Second method we bind this DataGrid with using XMLDataProvider to load and bind xml file to WPF Data Grid. XmlDataProvider exposes Source and XPath properties. Source property can be used to provide xml file name while XPath property used to specify the element name to generate collection.
First step we take Folder which name is Data and add Student.xml as shown in below picture


Code for Student.xml………………..

<?xml version="1.0" encoding="utf-8" ?>
<Students>
<Student RollNo="1" Name="Somesh Katiyar" Course="B.Tech" City="Kanpur" MobileNo="9911992345" />
<Student RollNo="2" Name="Manoj Yadav" Course="M.A." City="Ballia" MobileNo="3411992345" />
<Student RollNo="3" Name="Kush Tiwari" Course="Mca" City="Varanasi" MobileNo="7711992345" />
<Student RollNo="4" Name="Udayan Maiti" Course="M.Tech" City="Kolkatta" MobileNo="9900992345" />
<Student RollNo="5" Name="Vinay Singh" Course="B.A" City="Noida" MobileNo="9911789234" />
</Students>


Code with Window1.xaml ………………………………

<Window x:Class="BindDataGridinWPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="400">
<Window.Resources>
<XmlDataProvider Source="Data\Student.xml" XPath="Students" x:Key="stu" />
</Window.Resources>
<Grid Width="325" Height="150" Background="Azure" Margin="11,10,10,10">
<DataGrid Name="StudentGrid" AutoGenerateColumns="False" ItemsSource="{Binding Source={StaticResource stu},XPath=*}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath=@RollNo}" Header="RollNo" />
<DataGridTextColumn Binding="{Binding XPath=@Name}" Header="Name" />
<DataGridTextColumn Binding="{Binding XPath=@Course}" Header="Course" />
<DataGridTextColumn Binding="{Binding XPath=@City}" Header="City" />
<DataGridTextColumn Binding="{Binding XPath=@MobileNo}" Header="Mobile No" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>

Result:


Thrid Method…………………………….

Third method we bind this DataGrid with using Ado.Net concept  to load and bind table records to WPF Data Grid…………………………..

-- Sql Query.........................

create  database WpfConcept
 use WpfConcept
 create table Student(RollNo int primary key,Name varchar(50),Gender varchar(50),Course varchar(50))

Code with Window2.xaml ………………………………

<Window x:Class="BindDataGridinWPF.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Bind DataGrid with Sql Table" Height="300" Width="300">
<Grid>
<DataGrid x:Name="datagrid1" Width="235" Height="150" Background="Salmon" />
</Grid>
</Window>

Code with Window2.xaml.cs ………………………………
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
// using this  namespace
using System.Data;
using System.Data.SqlClient;

namespace BindDataGridinWPF
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>

public partial class Window2 : Window
{

public Window2()
{
InitializeComponent();
this.Loaded += Window2_Loaded;
}

void Window2_Loaded(object sender, RoutedEventArgs e)
{

SqlConnection con = new SqlConnection("Data Source=KUSH-PC;Initial Catalog=WpfConcept;User ID=sa;Password=tiwari");

SqlCommand cmd = new SqlCommand("Select * from Student", con);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds, "Student");
//datagrid.ItemsSource = ds.Tables["MyDetails"].DefaultView;
datagrid1.ItemsSource = new DataView(ds.Tables["Student"]);

}
}
}

Result:

0 comments:

Post a Comment