Demonstration of ListView Contents WPF in VB.NET

This article shows how to create a ListView control that uses a GridView view mode to display the contents.
  • 3636

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.