Export (0) Print
Expand All

GridView Class

Updated: July 2008

Represents a view mode that displays data items in columns for a ListView control.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

[StyleTypedPropertyAttribute(Property = "ColumnHeaderContainerStyle", StyleTargetType = typeof(GridViewColumnHeader))]
[ContentPropertyAttribute("Columns")]
public class GridView : ViewBase, IAddChild
<GridView>
  Columns
</GridView>

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.

ListView with GridView output

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<T> class. Similarly, you can use other Collection<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.

The following example shows how to define a ListView control that implements a GridView as its View.

  <ListView ItemsSource="{Binding Source={StaticResource EmployeeData}, 
                         XPath=Employee}">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="First Name" 
                      DisplayMemberBinding="{Binding XPath=FirstName}" />
      <GridViewColumn Header="Last Name" 
                      DisplayMemberBinding="{Binding XPath=LastName}" />
      <GridViewColumn Header="Favorite City"
                      DisplayMemberBinding="{Binding XPath=FavoriteCity}"/>
    </GridView>
  </ListView.View>
</ListView>

More Code

How to: Display ListView Contents by Using a GridView This example shows how to define a GridView view mode for a ListView control.
How to: Use Templates to Style a ListView That Uses GridView This example shows how to use the DataTemplate and Style objects to specify the appearance of a ListView control that uses a GridView view mode.
How to: Sort a GridView Column When a Header Is Clicked This example shows how to create a ListView control that implements a GridView view mode and sorts the data content when a user clicks a column header.
How to: Create a Custom View Mode for a ListView This example shows how to create a custom View mode for a ListView control.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Date

History

Reason

July 2008

Added new members: ColumnHeaderStringFormat property, ColumnHeaderStringFormatProperty field.

SP1 feature change.

Community Additions

ADD
Show:
© 2014 Microsoft