The GridView class and its supporting classes provide the infrastructure to display data items that are specified for a ListView control in a series of columns. The columns have column headers, which are buttons that are derived from ButtonBase, and you can reorder the columns by using drag-and-drop operations. Note that the columns of a GridView display data and do not provide direct access to the source of the data.
To specify a GridView as the view mode for a ListView, set the View property to a GridView object.
The GridView class is derived from ViewBase. You can define custom views by inheriting from the ViewBase class that provides the supporting elements for a view. For more information, see How to: Create a Custom View Mode for a ListView.
The following illustration shows an example of a ListView that uses a GridView.
.jpg)
The columns in a GridView are defined as GridViewColumn objects. In Extensible Application Markup Language (XAML), you can define GridViewColumn objects as child elements of the GridView. In code, you can add a GridViewColumn to the GridView by using the Columns property and the Add method that is defined for the Collection<(Of <(T>)>) class. Similarly, you can use other Collection<(Of <(T>)>) methods such as Remove and Insert to modify the columns in a GridView.
The following example shows how to define the columns of a GridView. For the complete sample, see ListView That Uses a GridView Sample.
GridView myGridView = new GridView();
myGridView.AllowsColumnReorder = true;
myGridView.ColumnHeaderToolTip = "Employee Information";
GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("FirstName");
gvc1.Header = "FirstName";
gvc1.Width = 100;
myGridView.Columns.Add(gvc1);
GridViewColumn gvc2 = new GridViewColumn();
gvc2.DisplayMemberBinding = new Binding("LastName");
gvc2.Header = "Last Name";
gvc2.Width = 100;
myGridView.Columns.Add(gvc2);
GridViewColumn gvc3 = new GridViewColumn();
gvc3.DisplayMemberBinding = new Binding("EmployeeNumber");
gvc3.Header = "Employee No.";
gvc3.Width = 100;
myGridView.Columns.Add(gvc3);
<GridView AllowsColumnReorder="true"
ColumnHeaderToolTip="Employee Information">
<GridViewColumn DisplayMemberBinding=
"{Binding Path=FirstName}"
Header="First Name" Width="100"/>
<GridViewColumn DisplayMemberBinding=
"{Binding Path=LastName}"
Width="100">
<GridViewColumnHeader>Last Name
<GridViewColumnHeader.ContextMenu>
<ContextMenu MenuItem.Click="LastNameCM_Click"
Name="LastNameCM">
<MenuItem Header="Ascending" />
<MenuItem Header="Descending" />
</ContextMenu>
</GridViewColumnHeader.ContextMenu>
</GridViewColumnHeader>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding=
"{Binding Path=EmployeeNumber}"
Header="Employee No." Width="100"/>
</GridView>
To style the rows in a GridView, define a style for the ListViewItem controls in the ListView. For an example of how to style rows in a GridView view, see the ListView with Styled ListViewItems Sample sample.
To add visual elements, such as a CheckBox or Button control, to a GridView, use templates or styles. For an example, see How to: Create ListViewItems with a CheckBox.