Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » WPF » Data Binding in WPF ListView

Data Binding in WPF ListView

This article shows how to extract data from a database and show in a WPF ListView control.

Author Rank :
Page Views : 115311
Downloads : 1776
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
Sql Server Binding in WPF.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


In this article, I am going to explain how to extract data from database and how to show data on a page using WPF ListView control.

 

I am using Northwind database, you can use whatever you want, and only you have to change is the connection string, your SQL string, and the binding properties in XAML code.

 

Here is the. xaml code:

 

<Grid x:Name="Grid1">

  <ListView Name="ListViewEmployeeDetails" Margin="4,20,40,100" ItemTemplate="{DynamicResource EmployeeTemplate}" ItemsSource="{Binding Path=Table}">

    <ListView.Background>

      <LinearGradientBrush>

        <GradientStop Color="Gray" Offset="0"/>           

      </LinearGradientBrush>

    </ListView.Background>

    <ListView.View>         

      <GridView>

        <GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding Path=EmployeeID}"/>

        <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}"/>

        <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}"/>

        <GridViewColumn Header="BirthDate" DisplayMemberBinding="{Binding Path=BirthDate}"/>

        <GridViewColumn Header="City" DisplayMemberBinding="{Binding Path=City}"/>

        <GridViewColumn Header="Country" DisplayMemberBinding="{Binding Path=Country}"/>

      </GridView>

    </ListView.View>

  </ListView>

</Grid>

 

Here is .cs code:

 

SqlConnection con = new SqlConnection();

SqlDataAdapter ad = new SqlDataAdapter();

SqlCommand cmd = new SqlCommand();

String str = "SELECT EmployeeID, FirstName, LastName, BirthDate, City, Country FROM Employees";

cmd.CommandText = str;

ad.SelectCommand = cmd;

con.ConnectionString = "Data Source=localhost; Initial Catalog=Northwind; Integrated Security=True";

cmd.Connection = con;

DataSet ds = new DataSet();

ad.Fill(ds);

ListViewEmployeeDetails.DataContext = ds.Tables[0].DefaultView;

con.Close();

 

Note:  Add two namespace.

 

using System.Data.SqlClient;

using System.Data;

 

Snapshot:

 

bindingwpf.JPG

I hope you will like this article. If yes drop me a line or write a comment below in the comments section.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by anver sadat on Sep 04, 2010
Hi,
   Nice Article on dynamic Binding in wpf Technology.I got clear difference  between static & dynamic binding Concept. U explained it in simple and smooth way. Be continue to sharing your ideas. More articles are required in wpf Tech. I am newbie in this. So please Guide me.
thnx and regards
 anver sadat
Contents added by a b on Dec 01, 2009
Thank's for the code, it helped me a lot !
 [Top] Rate this article
 
 About the author
 
Raj Kumar

Raj Kumar is a Microsoft MVP and Senior Software Engineer with lots of hands on experience using ASP.NET 2.0/3.5, AJAX, MVC, C#, Visual Basic .NET, SQL Server 2005/2008, Oracle, WPF, WCF, XAML and Silverlight. He has over 6 years of IT experience working most on Microsoft technologies. He holds Master's degree in Computer Science. When he is not writing code, he likes to write articles and play cricket.

Reach him at raj2511984@gmail.com 

 

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
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!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
hi raj by surineni On March 5, 2008
this example is very simple and easy to undestand how the binding works in wpf thanq very much shanthi
Reply | Email | Modify 
Hi I am getting this error by RAVIKUMAR On March 18, 2008
Hi, I am getting this error in my application, Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Reply | Email | Modify 
Re: Hi I am getting this error by Raj On October 22, 2008
This is not an application problem, this is version problem, you are missing something when installing. try to reinstall vs2008 again. hope that will work
Reply | Email | Modify 
Not working by Rangan On October 22, 2008
Hi, I followed the above mentioned procedure for displaying records from SQL Server 2008. The problem I have facing is that there are no records that are getting displayed in the grid inspite of 3 records existing in the database. I am not getting any errors too. The dataset is getting loaded with the records from the database. But the same is not getting bound to the grid control. Request your expert advice in this regard. Regards Rangan Parthasarathy
Reply | Email | Modify 
Re: Not working by Raj On October 22, 2008

first of all check your dataset has all records or not. if dataset has all records then keep same name of DisplayMemberBinding="{Binding Path=EmployeeID} name to your database column name like this. hope that will work

GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding Path=EmployeeID}"/>

Reply | Email | Modify 
Good article by anindya On February 27, 2010
Sir
many many thanks for this article.For a long time I was searching for such an article .
As I am a fresher.
Anindya Chatterjee
Reply | Email | Modify 
Retrieve a value from List View by rcthomas On May 19, 2010
If I were to click on a row of the list view, how could I retrieve a value, for example the Employee ID, from that particular row?

rcthomas@optonline.net
Reply | Email | Modify 
good example by Sushant On July 26, 2010
this is really a good example for beginners.really its helpful......................great job
Reply | Email | Modify 
hi by Batya On November 23, 2010
thanks
Reply | Email | Modify 
hi raj by saswat On January 3, 2011
this is a good example. I have a problem i have a grid view and want to vertical scroll in down to up direction. Thanks in advance.
Reply | Email | Modify 
Not working by jason On May 1, 2011
Thanks for this great article, it's nice to see some real stuff in WPF. However, it is not working for me. I downloaded and extracted the files, and without changing anything, I ran the app, and it gives me this error: 'The invocation of the constructor on type 'Sql_Server_Binding_in_WPF.Window1' that matches the specified binding constraints threw an exception.' Line number '3' and line position '5'. Can someone please explain why this error is happening? Thanks!
Reply | Email | Modify 
Not working with Oracle tables. by mukeshc.patel On November 10, 2011
Hi Raj, this code does not work if the dataset is filled with Oracle data. Any help is appreciated. Thanks.
Reply | Email | Modify 
Adding rows by Georges On December 19, 2011
Dear Raj, I want to add a record to a list view in wpf application when I click on an add button, and then saving the record into database I'm working on visual studio 2010 .. .Net Framework 3.5 and sql server 2008 any idea? Thank you
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.