WPF - Gridview

A GridView is a control that displays data items in rows and columns. Actually a ListView displays data. By default, it contains a GridView. The hierarchical inheritance of GridView class is as follows −
Background
Gets or sets a brush that provides the background of the control. (Inherited from Control)
BorderThickness
Gets or sets the border thickness of a control. (Inherited from Control)
DataContext
Gets or sets the data context for a FrameworkElement when it participates in data binding. (Inherited from FrameworkElement)
FontFamily
Gets or sets the font used to display text in the control. (Inherited from Control)
FontSize
Gets or sets the size of the text in this control. (Inherited from Control)
FontStyle
Gets or sets the style in which the text is rendered. (Inherited from Control)
FontWeight
Gets or sets the thickness of the specified font. (Inherited from Control)
Foreground
Gets or sets a brush that describes the foreground color. (Inherited from Control)
GroupStyle
Gets a collection of GroupStyle objects that define the appearance of each level of groups. (Inherited from ItemsControl)
Header
Gets or sets the content for the list header. (Inherited from ListViewBase)
Height
Gets or sets the suggested height of a FrameworkElement. (Inherited from FrameworkElement)
HorizontalAlignment
Gets or sets the horizontal alignment characteristics that are applied to a FrameworkElement when it is composed in a layout parent, such as a panel or items control. (Inherited from FrameworkElement)
HorizontalContentAlignment
Gets or sets the horizontal alignment of the control's content. (Inherited from Control)
Items
Gets the collection used to generate the content of the control. (Inherited from ItemsControl)
ItemsSource
Gets or sets an object source used to generate the content of the ItemsControl. (Inherited from ItemsControl)
ItemTemplate
Gets or sets the DataTemplate used to display each item. (Inherited from ItemsControl)
Margin
Gets or sets the outer margin of a FrameworkElement. (Inherited from FrameworkElement)
Name
Gets or sets the identifying name of the object. When a XAML processor creates the object tree from XAML markup, run-time code can refer to the XAML-declared object by this name. (Inherited from FrameworkElement)
Opacity
Gets or sets the degree of the object's opacity. (Inherited from UIElement)
Resources
Gets the locally defined resource dictionary. In XAML, you can establish resource items as child object elements of a frameworkElement.Resources property element, through XAML implicit collection syntax. (Inherited from FrameworkElement)
SelectedIndex
Gets or sets the index of the selected item. (Inherited from Selector)
SelectedItem
Gets or sets the selected item. (Inherited from Selector)
SelectedItems
Gets the currently selected items. (Inherited from ListViewBase)
SelectedRanges
Gets a collection of ItemIndexRange objects that describe the currently selected items in the list. (Inherited from ListViewBase)
SelectedValue
Gets or sets the value of the selected item, obtained by using the SelectedValuePath. (Inherited from Selector)
Style
Gets or sets an instance Style that is applied for this object during layout and rendering. (Inherited from FrameworkElement)
VerticalAlignment
Gets or sets the vertical alignment characteristics that are applied to a FrameworkElement when it is composed in a parent object such as a panel or items control. (Inherited from FrameworkElement)
VerticalContentAlignment
Gets or sets the vertical alignment of the control's content. (Inherited from Control)
Width
Gets or sets the width of a FrameworkElement. (Inherited from FrameworkElement)
DataContextChanged
Occurs when the value of the FrameworkElement.DataContext property changes. (Inherited from FrameworkElement)
DragEnter
Occurs when the input system reports an underlying drag event with this element as the target. (Inherited from UIElement)
DragLeave
Occurs when the input system reports an underlying drag event with this element as the origin. (Inherited from UIElement)
DragOver
Occurs when the input system reports an underlying drag event with this element as the potential drop target. (Inherited from UIElement)
DragStarting
Occurs when a drag operation is initiated. (Inherited from UIElement)
Drop
Occurs when the input system reports an underlying drop event with this element as the drop target. (Inherited from UIElement)
ImageFailed
Occurs when there is an error associated with image retrieval or format.
ImageOpened
Occurs when the image source is downloaded and decoded with no failure. You can use this event to determine the natural size of the image source.
KeyDown
Occurs when a keyboard key is pressed while the UIElement has focus. (Inherited from UIElement)
KeyUp
when a keyboard key is released while the UIElement has focus. (Inherited from UIElement)
Arrange
Positions child objects and determines a size for a UIElement. Parent objects that implement custom layout for their child elements should call this method from their layout override implementations to form a recursive layout update. (Inherited from UIElement)
ClearValue
Clears the local value of a dependency property. (Inherited from DependencyObject)
FindName
Retrieves an object that has the specified identifier name. (Inherited from FrameworkElement)
GetValue
Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject)
ReadLocalValue
Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject)
SetBinding
Attaches a binding to a FrameworkElement, using the provided binding object. (Inherited from FrameworkElement)
SetValue
Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject)
Let’s take an example to understand the concept better. Start by creating a new WPF project with the name WPFGridView.
Let’s take an example to understand the concept better. Start by creating a new WPF project with the name WPFGridView.
Drag a grid view control from the Toolbox.
Drag a grid view control from the Toolbox.
The following example shows the data in grid like table.
The following example shows the data in grid like table.
The following XAML code creates and implements a GridView.
The following XAML code creates and implements a GridView.
<Window x:Class = "WPFGridView.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "MainWindow" Height = "350" Width = "525">
	
   <Grid> 
      <ListView HorizontalAlignment = "Left" Height = "299" Margin = "10,10,0,0" 
         VerticalAlignment = "Top" Width = "497"Name = "MenList">
			
         <ListView.View>
            <GridView> 
               <GridViewColumn Header = "Name" DisplayMemberBinding = "{Binding Name}" 
                  Width = "100"/> 
						
               <GridViewColumn Header = "ID" DisplayMemberBinding = "{Binding ID}" 
                  Width = "100"/>
						
               <GridViewColumn Header = "Age" DisplayMemberBinding = "{Binding Age}" 
                  Width = "100"/>
						
            </GridView> 
         </ListView.View>
			
      </ListView> 
   </Grid> 
	
</Window>
Here is the C# implementation in which person class is implemented.
using System; 
using System.Windows; 
using System.Windows.Controls;
  
namespace WPFGridView { 
   /// <summary> 
      /// Interaction logic for MainWindow.xaml 
   /// </summary> 
	
   public partial class MainWindow : Window { 
	
      public MainWindow() { 
         InitializeComponent(); 
			
         MenList.Items.Add(new Person() {Name = "Ali", ID = "123A", Age = 20 }); 
         MenList.Items.Add(new Person() {Name = "Akram",ID= "456X", Age = 35 }); 
         MenList.Items.Add(new Person() {Name = "Salman",ID="333E", Age = 49 }); 
      } 
   }
	
   class Person { 
      public string Name { get; set; } 
      public string ID { get; set; } 
      public int Age { get; set; } 
   } 
	
}
When you compile and execute the above code, it will produce the following output.
We recommend that you execute the above example code and try the other properties and events of GridView.

    31 Lectures 
     2.5 hours 

 Anadi Sharma

    30 Lectures 
     2.5 hours 

 Taurius Litvinavicius
 Print
 Add Notes
 Bookmark this page