ARTICLE

Data Binding in WPF ListView

Posted by Raj Kumar Articles | WPF using VB.NET January 14, 2009
This article shows how to extract data from a database and show in a WPF ListView control.
 
Reader Level:

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 .VB code:

 

         Dim con As SqlConnection = New SqlConnection()
        
Dim ad As SqlDataAdapter = New SqlDataAdapter()
        
Dim cmd As SqlCommand = New SqlCommand()
        
Dim str As String = "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
        
Dim ds As DataSet = New DataSet()
         ad.Fill(ds)
         ListViewEmployeeDetails.DataContext = ds.Tables(0).DefaultView
         con.Close()
 

Note:  Add two namespace.

 

Imports System.Data.SqlClient
Imports System.Data

 

Snapshot:

 

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

Login to add your contents and source code to this article
share this article :
post comment
 
Posted by Raj Kumar Mar 05, 2009

Thanks for your help.  Is there somewhere I can go to that would show some sample code.

 

Posted by Randy Stewart Mar 05, 2009

use SQL Joins

Posted by Raj Kumar Mar 05, 2009

I am trying to do something similiar. How would you pull data from another table that is linked to each employee and display it underneath each employee record? Thanks

Posted by Randy Stewart Mar 03, 2009
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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.
Nevron Diagram
Become a Sponsor