Click here to Skip to main content
12,071,437 members (35,275 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked

Data binding in WPF DataGrid control using SQL Server database

, 10 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
In this article I will explain how to display data from a SQL Server database table in a WPF DataGrid control


Displaying data in a grid is a common task in a Windows application. In a Windows application we use the DataGridView control for this task. In WPF, we can use the DataGrid control for the same. In this article we will display the contents of the Employee table of the Pubs database in a WPF DataGrid control.

Using the code

Followings are the steps to achive the above task.

Step 1: 

Create a new WPF Application

Note: I am using Visual C# 2010 Express

Step 2:  

Add a new “Application Configuration File” with named “App.config” and your connection string

    <add connectionstring="Data Source=YourDataSource; User Id=YourUserName;Password=YourPassword; Initial Catalog=Pubs;" name="ConString"/>

Step 3: 

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

Step 4:   

Add a DataGrid control in the MainWindow.xaml file.

<datagrid name="grdEmployee" />  

Write following code in MainWindow.xaml.cs to bind data.

public MainWindow()

private void FillDataGrid()
    string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    string CmdString = string.Empty;
    using (SqlConnection con = new SqlConnection(ConString))
        CmdString = "SELECT emp_id, fname, lname, hire_date FROM Employee";
        SqlCommand cmd = new SqlCommand(CmdString, con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable("Employee");
        grdEmployee.ItemsSource = dt.DefaultView; 
Here first we loaded Employee database table in a DataTable object and then bind it to the DataGrid’s ItemsSource property.

Step 5:

After running the application we get the following output

ItemsSource does it all for us. It displays all the data from the underlying data source. But it has some limitations like you can see header of the columns are same as the database table column names. To give column’s header a user defined name you can change DataGrid definition as following code in MainWindow.xaml file

<datagrid alternatingrowremoved="#FFC4B0B0" height="400" width="350" autogeneratecolumns="False" name="grdEmployee">
        <datagridtextcolumn binding="{Binding FirstName}" width="100" header="First Name"/>
        <datagridtextcolumn binding="{Binding LastName}" width="100" header="Last Name"/>
        <datagridtextcolumn binding="{Binding Salary}" width="100" header="Salary"/>

Final Output:  


Points of Interest

When we set ItemsSource property of a DataGrid to a data source, it displays all all the columns with its default name. To give a user defined name to the DataGrid columns we have to set AutoGenerateColumns="False" and define its columns explicitly.


  • 9 April, 2012.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionSystem.TypeInitializationException Pin
Member 1165565715-May-15 8:23
memberMember 1165565715-May-15 8:23 
QuestionSource Code Pin
ExoCoding3-Mar-15 0:22
memberExoCoding3-Mar-15 0:22 
AnswerRe: Source Code Pin
Deepak_Sharma_3-Mar-15 1:44
memberDeepak_Sharma_3-Mar-15 1:44 
GeneralRe: Source Code Pin
ExoCoding3-Mar-15 2:48
memberExoCoding3-Mar-15 2:48 
QuestionTanks Pin
farhad.bagherlo27-Sep-14 4:46
memberfarhad.bagherlo27-Sep-14 4:46 
GeneralMy vote of 3 Pin
Member 1108217723-Sep-14 17:29
memberMember 1108217723-Sep-14 17:29 
Suggestionhow about update that tbale by adding/deleting editing rows? Pin
stelios198417-Jul-14 22:04
memberstelios198417-Jul-14 22:04 
QuestionError Pin
Member 1081830521-May-14 0:22
memberMember 1081830521-May-14 0:22 
QuestionConnection String Pin
Member 1075352816-Apr-14 3:59
memberMember 1075352816-Apr-14 3:59 
Questionconfiguartion element is not declared Pin
Member 1073299110-Apr-14 19:34
memberMember 1073299110-Apr-14 19:34 
QuestionConfiguration manager doesn't exist in current context Pin
Member 107329918-Apr-14 1:39
memberMember 107329918-Apr-14 1:39 
AnswerRe: Configuration manager doesn't exist in current context Pin
Member 1094067023-Oct-14 9:49
memberMember 1094067023-Oct-14 9:49 
QuestionConnection manager Pin
R.BinayRam3-Apr-14 3:41
memberR.BinayRam3-Apr-14 3:41 
AnswerRe: Connection manager Pin
Deepak_Sharma_4-Apr-14 7:10
memberDeepak_Sharma_4-Apr-14 7:10 
QuestionXamlParseException was unhandled error is coming Pin
Member 1028454127-Nov-13 19:39
professionalMember 1028454127-Nov-13 19:39 
QuestionThank you for sharing..But i have a question. Pin
Member 1040682617-Nov-13 2:27
memberMember 1040682617-Nov-13 2:27 
QuestionGreat article for displaying the data.... but... Pin
Bogus Exception14-Oct-13 5:53
memberBogus Exception14-Oct-13 5:53 
AnswerRe: Great article for displaying the data.... but... Pin
Deepak_Sharma_14-Oct-13 20:54
memberDeepak_Sharma_14-Oct-13 20:54 
GeneralRe: Great article for displaying the data.... but... Pin
Bogus Exception7-Mar-14 10:30
memberBogus Exception7-Mar-14 10:30 
General"Exception has been thrown by the target of an invocation." Pin
MAYANK GEETE11-Sep-13 1:36
professionalMAYANK GEETE11-Sep-13 1:36 
GeneralGreat Article for getting started with WPF and the DataGrid Pin
Guttorm Haaversen7-Sep-13 9:39
memberGuttorm Haaversen7-Sep-13 9:39 
GeneralRe: Great Article for getting started with WPF and the DataGrid Pin
Deepak_Sharma_14-Oct-13 21:24
memberDeepak_Sharma_14-Oct-13 21:24 
QuestionError Pin
Abhishek Durvasula4-Sep-13 20:33
memberAbhishek Durvasula4-Sep-13 20:33 
AnswerRe: Error Pin
Abhishek Durvasula4-Sep-13 21:15
memberAbhishek Durvasula4-Sep-13 21:15 
QuestionDataTable Pin
Member 101909725-Aug-13 9:42
memberMember 101909725-Aug-13 9:42 
AnswerRe: DataTable Pin
Member 101909725-Aug-13 10:04
memberMember 101909725-Aug-13 10:04 
GeneralMy vote of 5 Pin
Member 998831623-Jul-13 7:58
memberMember 998831623-Jul-13 7:58 
SuggestionBinding names and case sensitivity Pin
RickiB31-May-13 14:57
memberRickiB31-May-13 14:57 
QuestionScalar Query Pin
muralimahendra3-Oct-12 1:03
membermuralimahendra3-Oct-12 1:03 
QuestionGreat, simple example... however, it generated an runtime error about ConfiguraitonManager... Pin
youngtexas19-Apr-12 5:58
memberyoungtexas19-Apr-12 5:58 
AnswerRe: Great, simple example... however, it generated an runtime error about ConfiguraitonManager... Pin
Deepak_Sharma_19-Apr-12 6:24
memberDeepak_Sharma_19-Apr-12 6:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160208.1 | Last Updated 10 Apr 2012
Article Copyright 2012 by Deepak_Sharma_
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid