Click here to Skip to main content
14,870,621 members
Articles / Desktop Programming / WPF
Posted 10 Apr 2012


36 bookmarked

Data binding in WPF DataGrid control using SQL Server database

Rate me:
Please Sign up or sign in to vote.
4.84/5 (29 votes)
10 Apr 2012CPOL1 min read
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

Image 1

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

Image 2

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:  

Image 3 

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

Comments and Discussions

PraiseMy vote of 5 Pin
K)Hispanico17-May-18 22:21
MemberK)Hispanico17-May-18 22:21 
GeneralMy vote of 5 Pin
Member 1352218314-Nov-17 22:31
MemberMember 1352218314-Nov-17 22:31 
Questionerror 'Null Reference exception was unhandled by user code 'Object reference not set to an instance of an object.' Pin
EM_Y26-Apr-17 5:05
MemberEM_Y26-Apr-17 5:05 
PraiseRe: error 'Null Reference exception was unhandled by user code 'Object reference not set to an instance of an object.' Pin
EM_Y26-Apr-17 23:09
MemberEM_Y26-Apr-17 23:09 
BugFillDataGrid() method cannot be called Pin
Saurabh Solanki20-Jun-16 3:44
MemberSaurabh Solanki20-Jun-16 3:44 
GeneralRe: FillDataGrid() method cannot be called Pin
Deepak_Sharma_20-Jun-16 19:46
MemberDeepak_Sharma_20-Jun-16 19:46 
GeneralRe: FillDataGrid() method cannot be called Pin
Saurabh Solanki20-Jun-16 19:55
MemberSaurabh Solanki20-Jun-16 19:55 
GeneralRe: FillDataGrid() method cannot be called Pin
Saurabh Solanki25-Jun-16 2:36
MemberSaurabh Solanki25-Jun-16 2:36 
PraiseThis one endly work for me Pin
Member 1190232229-May-16 3:02
MemberMember 1190232229-May-16 3:02 
QuestionSystem.TypeInitializationException Pin
enavuio15-May-15 7:23
Memberenavuio15-May-15 7:23 
QuestionSource Code Pin
ExoCoding2-Mar-15 23:22
MemberExoCoding2-Mar-15 23:22 
AnswerRe: Source Code Pin
Deepak_Sharma_3-Mar-15 0:44
MemberDeepak_Sharma_3-Mar-15 0:44 
GeneralRe: Source Code Pin
ExoCoding3-Mar-15 1:48
MemberExoCoding3-Mar-15 1:48 
QuestionTanks Pin
farhad.bagherlo27-Sep-14 3:46
Memberfarhad.bagherlo27-Sep-14 3:46 
GeneralMy vote of 3 Pin
Member 1108217723-Sep-14 16:29
MemberMember 1108217723-Sep-14 16:29 
Suggestionhow about update that tbale by adding/deleting editing rows? Pin
stelios198417-Jul-14 21:04
Memberstelios198417-Jul-14 21:04 
QuestionError Pin
Member 1081830520-May-14 23:22
MemberMember 1081830520-May-14 23:22 
QuestionConnection String Pin
Member 1075352816-Apr-14 2:59
MemberMember 1075352816-Apr-14 2:59 
Questionconfiguartion element is not declared Pin
Member 1073299110-Apr-14 18:34
MemberMember 1073299110-Apr-14 18:34 
QuestionConfiguration manager doesn't exist in current context Pin
Member 107329918-Apr-14 0:39
MemberMember 107329918-Apr-14 0:39 
AnswerRe: Configuration manager doesn't exist in current context Pin
Member 1094067023-Oct-14 8:49
MemberMember 1094067023-Oct-14 8:49 
QuestionConnection manager Pin
R.BinayRam3-Apr-14 2:41
MemberR.BinayRam3-Apr-14 2:41 
AnswerRe: Connection manager Pin
Deepak_Sharma_4-Apr-14 6:10
MemberDeepak_Sharma_4-Apr-14 6:10 
QuestionXamlParseException was unhandled error is coming Pin
Member 1028454127-Nov-13 18:39
professionalMember 1028454127-Nov-13 18:39 
QuestionThank you for sharing..But i have a question. Pin
Member 1040682617-Nov-13 1:27
MemberMember 1040682617-Nov-13 1:27 

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.