This topic has not yet been rated - Rate this topic

GridView Class

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

The GridView type exposes the following members.

  Name Description
Public method GridView Initializes a new instance of the GridView class.
Top
  Name Description
Public property AllowsColumnReorder Gets or sets whether columns in a GridView can be reordered by a drag-and-drop operation.
Public property ColumnHeaderContainerStyle Gets or sets the style to apply to column headers.
Public property ColumnHeaderContextMenu Gets or sets a ContextMenu for the GridView.
Public property ColumnHeaderStringFormat Gets or sets a composite string that specifies how to format the column headers of the GridView if they are displayed as strings.
Public property ColumnHeaderTemplate Gets or sets a template to use to display the column headers.
Public property ColumnHeaderTemplateSelector Gets or sets the selector object that provides logic for selecting a template to use for each column header.
Public property ColumnHeaderToolTip Gets or sets the content of a tooltip that appears when the mouse pointer pauses over one of the column headers.
Public property Columns Gets the collection of GridViewColumn objects that is defined for this GridView.
Protected property DefaultStyleKey Gets the reference for the default style for the GridView. (Overrides ViewBase.DefaultStyleKey.)
Public property DependencyObjectType Gets the DependencyObjectType that wraps the CLR type of this instance.  (Inherited from DependencyObject.)
Public property Dispatcher Gets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Public property Static member GridViewItemContainerStyleKey Gets the key that references the style that is defined for each ListViewItem in a GridView.
Public property Static member GridViewScrollViewerStyleKey Gets the key that references the style that is defined for the ScrollViewer control that encloses the content that is displayed by a GridView.
Public property Static member GridViewStyleKey Gets the key that references the style that is defined for the GridView.
Public property IsSealed Gets a value that indicates whether this instance is currently sealed (read-only). (Inherited from DependencyObject.)
Protected property ItemContainerDefaultStyleKey Gets the reference to the default style for the container of the data items in the GridView. (Overrides ViewBase.ItemContainerDefaultStyleKey.)
Top
  Name Description
Public attached property ColumnCollection Gets or sets the attached property that contains the GridViewColumnCollection.
Top
  Name Description
Protected method AddChild Infrastructure. Adds a GridViewColumn object to a GridView.
Protected method AddText Infrastructure. Not supported.
Public method CheckAccess Determines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Protected method ClearItem Removes all settings, bindings, and styling from a ListViewItem. (Overrides ViewBase.ClearItem(ListViewItem).)
Public method ClearValue(DependencyProperty) Clears the local value of a property. The property to be cleared is specified by a DependencyProperty identifier. (Inherited from DependencyObject.)
Public method ClearValue(DependencyPropertyKey) Clears the local value of a read-only property. The property to be cleared is specified by a DependencyPropertyKey. (Inherited from DependencyObject.)
Public method CoerceValue Coerces the value of the specified dependency property. This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject. (Inherited from DependencyObject.)
Public method Equals Determines whether a provided DependencyObject is equivalent to the current DependencyObject. (Inherited from DependencyObject.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected method GetAutomationPeer Gets the AutomationPeer implementation for this GridView object. (Overrides ViewBase.GetAutomationPeer(ListView).)
Public method Static member GetColumnCollection Gets the contents of the ColumnCollection attached property.
Public method GetHashCode Gets a hash code for this DependencyObject. (Inherited from DependencyObject.)
Public method GetLocalValueEnumerator Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject. (Inherited from DependencyObject.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetValue Returns the current effective value of a dependency property on this instance of a DependencyObject. (Inherited from DependencyObject.)
Public method InvalidateProperty Re-evaluates the effective value for the specified dependency property (Inherited from DependencyObject.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method OnPropertyChanged Invoked whenever the effective value of any dependency property on this DependencyObject has been updated. The specific dependency property that changed is reported in the event data. (Inherited from DependencyObject.)
Protected method PrepareItem Prepares a ListViewItem for display according to the definition of this GridView object. (Overrides ViewBase.PrepareItem(ListViewItem).)
Public method ReadLocalValue Returns the local value of a dependency property, if it exists. (Inherited from DependencyObject.)
Public method Static member SetColumnCollection Sets the contents of the ColumnCollection attached property.
Public method SetCurrentValue Sets the value of a dependency property without changing its value source. (Inherited from DependencyObject.)
Public method SetValue(DependencyProperty, Object) Sets the local value of a dependency property, specified by its dependency property identifier. (Inherited from DependencyObject.)
Public method SetValue(DependencyPropertyKey, Object) Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property. (Inherited from DependencyObject.)
Public method Static member ShouldSerializeColumnCollection Determines whether to serialize the ColumnCollection attached property.
Protected method ShouldSerializeProperty Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property. (Inherited from DependencyObject.)
Public method ToString Returns the string representation of the GridView object. (Overrides Object.ToString().)
Public method VerifyAccess Enforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Top
  Name Description
Public field Static member AllowsColumnReorderProperty Identifies the AllowsColumnReorder dependency property.
Public field Static member ColumnCollectionProperty Identifies the ColumnCollectionProperty attachedproperty.
Public field Static member ColumnHeaderContainerStyleProperty Identifies the ColumnHeaderContainerStyle dependency property.
Public field Static member ColumnHeaderContextMenuProperty Identifies the ColumnHeaderContextMenu dependency property.
Public field Static member ColumnHeaderStringFormatProperty Identifies the ColumnHeaderStringFormat dependency property.
Public field Static member ColumnHeaderTemplateProperty Identifies the ColumnHeaderTemplate dependency property.
Public field Static member ColumnHeaderTemplateSelectorProperty Identifies the ColumnHeaderTemplateSelector dependency property.
Public field Static member ColumnHeaderToolTipProperty Identifies the ColumnHeaderToolTip dependency property.
Top
  Name Description
Explicit interface implemetation Private method IAddChild.AddChild Infrastructure. Adds a child object.
Explicit interface implemetation Private method IAddChild.AddText Infrastructure. Adds the text content of a node to the object.
Top

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.



            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.

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.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ