ARTICLE

Demonstration of ListView Contents in WPF

Posted by Purushottam Rathore Articles | WPF January 14, 2009
This article shows how to create a ListView control that uses a GridView view mode to display the contents.
Reader Level:
Download Files:
 

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:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Configuration;
using System.Xml;

namespace WpfApplication2
{
    public partial class Window1 : Window
    {
        DataSet ds = new DataSet();
        public Window1()
        {
            InitializeComponent();
        }

        private
void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            Grid_Loaded();
        }

        private
void Grid_Loaded()
        {
            SqlConnection con = new SqlConnection("Data source=.;initial catalog=Puru; uid=sa;password=;");

            SqlCommand cmd = new SqlCommand("select top 20 User_LoginID,User_EMail, convert(varchar(12),IsCreatedDate,107)IsCreatedDate from tbl_User", con);

            SqlDataAdapter sqlDa = new SqlDataAdapter();
            sqlDa.SelectCommand = cmd;
            sqlDa.Fill(ds);
            lstView.DataContext = ds.Tables[0].DefaultView;
        }
    }
}

Output:

Login to add your contents and source code to this article
comments
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter