Click here to Skip to main content
15,398,850 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

QuestionError Pin
Abhishek Durvasula4-Sep-13 19:33
MemberAbhishek Durvasula4-Sep-13 19:33 
AnswerRe: Error Pin
Abhishek Durvasula4-Sep-13 20:15
MemberAbhishek Durvasula4-Sep-13 20:15 
QuestionDataTable Pin
Member 101909725-Aug-13 8:42
professionalMember 101909725-Aug-13 8:42 
AnswerRe: DataTable Pin
Member 101909725-Aug-13 9:04
professionalMember 101909725-Aug-13 9:04 
GeneralMy vote of 5 Pin
Member 998831623-Jul-13 6:58
MemberMember 998831623-Jul-13 6:58 
SuggestionBinding names and case sensitivity Pin
RickiB31-May-13 13:57
MemberRickiB31-May-13 13:57 
QuestionScalar Query Pin
muralimahendra3-Oct-12 0:03
Membermuralimahendra3-Oct-12 0:03 
QuestionGreat, simple example... however, it generated an runtime error about ConfiguraitonManager... Pin
youngtexas19-Apr-12 4:58
Memberyoungtexas19-Apr-12 4:58 
Can you upload the project with source code? I could not get past over the ConfiguationManager error. "Configuration system failed to initialize". I googled and found something like "<configsections> has to be the first section in <configuration> section". I tried it and it did not work for either. Thanks in advance!!!
AnswerRe: Great, simple example... however, it generated an runtime error about ConfiguraitonManager... Pin
Deepak_Sharma_19-Apr-12 5:24
MemberDeepak_Sharma_19-Apr-12 5: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.