SIGN UP MEMBER LOGIN:    
ARTICLE

DataGrid in WPF

Posted by Purushottam Rathore Articles | WPF with C# October 06, 2010
In this article you will learn how to bind records from the database in DataGrid using WPF
Reader Level:

DataGrid in WPF:

 

How to bind records in DataGrid using WPF?

 

MainWindow.xaml:

 

<Window x:Class="DataGridInWPF.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="250" Width="550" Loaded="Window_Loaded">

    <Grid>

        <DataGrid HorizontalAlignment="Center" Name="dataGrid1"

                  AutoGenerateColumns="False"

                  CanUserResizeRows="True"

                  CanUserResizeColumns="True"

                  CanUserReorderColumns="True">

            <DataGrid.Columns>

                <DataGridTextColumn Binding="{Binding  Path=CustomerID}" Header="ID" MaxWidth="50" />

                <DataGridTextColumn Binding="{Binding Path=CompanyName}" Header="Company" MaxWidth="100" />

                <DataGridTextColumn Binding="{Binding Path=ContactName}" Header="Name" MaxWidth="100" />

                <DataGridTextColumn Binding="{Binding Path=City}" Header="City" MaxWidth="100" />

                <DataGridTextColumn Binding="{Binding Path=Country}" Header="Country" MaxWidth="100" />

                <DataGridTextColumn Binding="{Binding Path=Phone}" Header="Phone" MaxWidth="100" />

            </DataGrid.Columns>

        </DataGrid>

    </Grid>

</Window>

 

DataGrid.png 

 

Figure 1: Display DatGrid in design format.

 

 

MainWindow.xaml.cs:

 

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;

using System.Data.SqlClient;

 

namespace DataGridInWPF

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        SqlConnection MyConnection;

        SqlCommand MyCommand;

        SqlDataAdapter MyAdapter;

        DataTable MyTable;

 

        public MainWindow()

        {

            InitializeComponent();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            BindGrid();

        }

        private void BindGrid()

        {

            MyConnection = new SqlConnection();

            MyConnection.ConnectionString = "SERVER=Puru;Uid=sa; pwd=wintellect;Database=TestData;";

            if (MyConnection.State == ConnectionState.Closed)

                MyConnection.Open();

            MyCommand = new SqlCommand();

            MyCommand.CommandText = "Select CustomerID,CompanyName,ContactName,Country,City,Phone from CustomerDetail";

            MyCommand.CommandType = CommandType.Text;

            MyCommand.Connection = MyConnection;

            MyTable = new DataTable();

            MyAdapter = new SqlDataAdapter();

            MyAdapter.SelectCommand = MyCommand;

            MyAdapter.Fill(MyTable);

            dataGrid1.ItemsSource = MyTable.DefaultView;

        }    

    }

}

Login to add your contents and source code to this article
share this article :
post comment
 

I have searched for hours trying to find a great example like this that shows what I needed to learn. I am new to WPF... one of my big problems is that there is so many different ways people are doing something (C#, VB, ASP.net, Winforms vs. WPF) and that it gets confusing; depending upon which platform you are using there are different methods. I am using C## Visual Studio 2010, using WPF, and I want to read/write to a SQL database. If you can recommend any books that would be great. Currently I'm using "Visual C## 2010 Step by Step" by John Sharp and "WPF 4 Unleashed" is in the mail from Amazon. Thank you, this helped a lot.

Posted by Ernie Cooper Mar 15, 2011

The data grid is an interesting control

Posted by sean kofi Feb 14, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor