The DataGrid control also supports editing and many other features that you might not always need. This means that the DataGrid does take a little more time to render the data. If you want to just display data (see Figure 1) in a grid format, then a ListView works quite well for this task. Of course, you will need to create the columns for the ListView, but with just a little generic code, you can create the columns on the fly just like the WPF Toolkit’s DataGrid.
Figure 1: A List of Data using a ListView
A Simple ListView Control
>ListView x:Name="lstData"
ItemsSource="{Binding}">
>ListView.View>
>GridView>
>GridViewColumn Header="Product ID"
Width="Auto"
DisplayMemberBinding="{Binding Path=ProductId}" />
>GridViewColumn Header="Product Name"
Width="Auto"
DisplayMemberBinding="{Binding Path=ProductName}" />
>GridViewColumn Header="Price"
Width="Auto"
DisplayMemberBinding="{Binding Path=Price}" />
>/GridView>
>/ListView.View>
>/ListView>
So, instead of creating the GridViewColumn’s in XAML, let’s learn to create them in code to create any amount of columns in a ListView.
Create GridViewColumn’s From Data Table
Listing 1 shows a sample of reading and XML file into a DataSet object. After reading the data a GridView object is created. You can then loop through the DataTable columns collection and create a GridViewColumn object for each column in the DataTable. Notice the DisplayMemberBinding property is set to a new Binding to the ColumnName in the DataTable.
C#
private void FirstSample()
{
// Read the data
DataSet ds = new DataSet();
ds.ReadXml(GetCurrentDirectory() + @"\Xml\Product.xml");
// Create the GridView
GridView gv = new GridView();
// Create the GridView Columns
foreach (DataColumn item in ds.Tables[0].Columns)
{
GridViewColumn gvc = new GridViewColumn();
gvc.DisplayMemberBinding = new Binding(item.ColumnName);
gvc.Header = item.ColumnName;
gvc.Width = Double.NaN;
gv.Columns.Add(gvc);
}
// Setup the GridView Columns
lstData.View = gv;
// Display the Data
lstData.DataContext = ds.Tables[0];
}
VB.NET
Private Sub FirstSample()
' Read the data
Dim ds As New DataSet()
ds.ReadXml(GetCurrentDirectory() & "\Xml\Product.xml")
' Create the GridView
Dim gv As New GridView()
' Create the GridView Columns
For Each item As DataColumn In ds.Tables(0).Columns
Dim gvc As New GridViewColumn()
gvc.DisplayMemberBinding = New Binding(item.ColumnName)
gvc.Header = item.ColumnName
gvc.Width = [Double].NaN
gv.Columns.Add(gvc)
Next
' Setup the GridView Columns
lstData.View = gv
' Display the Data
lstData.DataContext = ds.Tables(0)
End Sub
Listing 1: Loop through the DataTable columns collection to create GridViewColumn objects
A Generic Method for Creating a GridView
C#
private void DataTableSample()
{
// Read the data
DataSet ds = new DataSet();
ds.ReadXml(GetCurrentDirectory() + @"\Xml\Product.xml");
// Setup the GridView Columns
lstData.View =
WPFListViewCommon.CreateGridViewColumns(ds.Tables[0]);
lstData.DataContext = ds.Tables[0];
}
VB.NET
Private Sub DataTableSample()
' Read the data
Dim ds As New DataSet()
ds.ReadXml(GetCurrentDirectory() & "\Xml\Product.xml")
' Setup the GridView Columns
lstData.View = _
WPFListViewCommon.CreateGridViewColumns(ds.Tables(0))
lstData.DataContext = ds.Tables(0)
End Sub
Listing 2: Call a generic method to create GridViewColumns.
The CreateGridViewColumns Method
C#
public static GridView CreateGridViewColumns(DataTable dt)
{
// Create the GridView
GridView gv = new GridView();
gv.AllowsColumnReorder = true;
// Create the GridView Columns
foreach (DataColumn item in dt.Columns)
{
GridViewColumn gvc = new GridViewColumn();
gvc.DisplayMemberBinding = new Binding(item.ColumnName);
gvc.Header = item.ColumnName;
gvc.Width = Double.NaN;
gv.Columns.Add(gvc);
}
return gv;
}
VB.NET
Public Shared Function CreateGridViewColumns _
(ByVal dt As DataTable) As GridView
' Create the GridView
Dim gv As New GridView()
gv.AllowsColumnReorder = True
' Create the GridView Columns
For Each item As DataColumn In dt.Columns
Dim gvc As New GridViewColumn()
gvc.DisplayMemberBinding = New Binding(item.ColumnName)
gvc.Header = item.ColumnName
gvc.Width = [Double].NaN
gv.Columns.Add(gvc)
Next
Return gv
End Function