ARTICLE

Demonstration of ListView Contents in WPF

Posted by Purushottam Rathore Articles | WPF using VB.NET September 22, 2010
This article shows how to create a ListView control that uses a GridView view mode to display the contents.
 
Reader Level:

This article demonstrates how to create the ListView Contents by using GridView and Extensible Application Markup Language (XAML) and also by using code and also demonstrates a specific feature of the Windows Presentation Foundation.

The ListView control is a great example of WPF to bind the data from the database- the possibilities are almost endless. This article on the ListView will hopefully useful for biggners. i am starting today with a simple grid based list view, showing how to create columns and some different ways of getting data into those columns.

Now You can define the view mode of a GridView by specifying GridViewColumn objects. The following example shows how to define GridViewColumn objects that bind to the data content that is specified for the ListView control. This GridView example specifies three GridViewColumn objects that map to the Name, EMail, and CreatedDate fields of the DataSource that is set as the ItemsSource of the ListView control.

Example: Write the following code on the window1.xaml file.

Window1.xaml:

<Window x:Class="WpfApplication2.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="ListView" Height="350" Width="400" Loaded="Window1_Loaded">

<Window.Resources>
    <
DataTemplate x:Key="FirstCell">
        <
StackPanel Orientation="Horizontal"></StackPanel>
    </
DataTemplate>
</
Window.Resources>
<
Grid>
    <
ListView ItemsSource="{Binding}" Name="lstView" Background="LightGray">
        <
ListView.View>
            <
GridView>
                <
GridView.Columns>
                    <
GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=User_LoginID}" Width="75"/>
                    <
GridViewColumn Header="EMail" DisplayMemberBinding="{Binding Path=User_EMail}" Width="200"/>
                    <
GridViewColumn Header="CreatedDate" DisplayMemberBinding="{Binding Path=IsCreatedDate}" Width="125"/>
                </
GridView.Columns>
            </
GridView>
        </
ListView.View>
    </
ListView>
</
Grid>
</
Window>

Window1.xaml.cs:

Imports System.Collections.Generic
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Linq
Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel
Imports System.Configuration
Imports System.Xml
Namespace WpfApplication2
    Partial Public Class Window1
        Inherits Window
        Private ds As New DataSet()
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Grid_Loaded()
        End Sub

        Private Sub Grid_Loaded()
            Dim con As New SqlConnection("Data source=.;initial catalog=Puru; uid=sa;password=;")
            Dim cmd As New SqlCommand("select top 20 User_LoginID,User_EMail, convert(varchar(12),IsCreatedDate,107)IsCreatedDate from tbl_User", con)
            Dim sqlDa As New SqlDataAdapter()
            sqlDa.SelectCommand = cmd
            sqlDa.Fill(ds)
            lstView.DataContext = ds.Tables(0).DefaultView
        End Sub
    End Class
End Namespace

Output:

Listview.JPG

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks for the post.. I was struggling to show records on the ListView from databse and your code helped to find a way

Following lines are missing in your code.

con.Open();
DataSet ds=new DataSet();

Posted by Rajeeshun Arul Sep 27, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • 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. Visit DynamicPDF here
    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.
Nevron Diagram
Become a Sponsor