Mastering WPF DataGrid in a Day: Hour 4 DataGrid Rows

In this Hour 4 of the Mastering WPF in 24 Hrs series I will talk about the DataGrid row header and rows visibility, row appearance and a few other properties.

Before continuing this article, I highly recommend reading these previous parts:

RowHeaderWidth

A DataGrid is a collection of rows and columns and each row and column can have a header. This header can be used to display text such as column and row titles.

The RowHeaderWidth property is used to set the row header width.

The code snippet in Listing 15 sets the RowHeaderWidth to 50.
  1. <DataGrid HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"   
  2.             Height="219" Width="465" Name="McDataGrid" AutoGenerateColumns="False"  
  3.                   RowHeaderWidth="50" />  
Listing 15

The output looks as in Figure 8.

row header
Figure 8

GridLinesVisibility

You can show or hide a DataGrid grid line using the GridLinesVisibility property. The GridLinesVisibility has the four values All, Horizontal, None and Vertical. If you want to show both the horizontal and the vertical lines, set the GridLinesVisibility to All. The None value hides both the horizontal and the vertical lines and the Horizontal and Vertical values display the horizontal and vertical lines only respectively. See Listing 16.

gridlines visibility

Listing 16

The code snipet in Listing 17 hides both row and column headers of a DataGrid.
  1. <DataGrid HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"   
  2.             Height="219" Width="509" Name="McDataGrid" AutoGenerateColumns="True"  
  3.             RowHeaderWidth="10" AlternatingRowBackground="LightGreen"  
  4.             GridLinesVisibility="Horizontal" />  
Listing 17

The output looks as in Figure 9.

main window
Figure 9

AlternatingRowBackground

The AlternatingRowBackground property is used to set and get the background color of every alternative row of a DataGrid.

The code snippet in Listing 18 sets the AlternatingRowBackground to LightGreen. Also note that I have changed the RowHeaderWidth to 10.
  1. <DataGrid HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"   
  2.             Height="219" Width="509" Name="McDataGrid" AutoGenerateColumns="True"  
  3.             RowHeaderWidth="10" AlternatingRowBackground="LightGreen" />  
Listing 18

The output looks as in Figure 10.

table
Figure 10

SelectionMode and SelectionUnit

The SelectionMode and SelectionUnit properties can be used to define the selection behavior for the DataGrid control. The SelectionMode property indicates how rows and cells are selected in the DataGrid. The SelectionMode has one of the following two values:
  • Extended: Multiple items can be selected at the same time.

  • Single: Only one item can be selected a time.
The SelectionUnit property indicates whether rows, cells, or both can be selected in the DataGrid. The SelectionUnit has one of the following three values:
  • Cell: Only cells are selected. Clicking a row or column header does nothing.

  • CellOrRowHeader: Cells and rows are selected. Clicking on a cell selects only the cell. Clicking a row header selects the full row.

  • FullRow: Click a cell or a row header to select the full row.

The code snippet in Listing 19 sets the SelectionUnit and SelectionMode properties to Cell and Extended.

  1. <DataGrid HorizontalAlignment="Left" Margin="4,5,0,0" VerticalAlignment="Top"   
  2.             Height="120" Width="510" Name="McDataGrid" AutoGenerateColumns="True"  
  3.             AlternatingRowBackground="LightGreen" GridLinesVisibility="Horizontal"  
  4.             ColumnWidth="*" SelectionUnit="Cell" SelectionMode="Extended"/>  
Listing 19

The output looks as in Figure 11. If you click on multiple cells with the CTRL key, you will see multiple cells are selected.

graphics program
Figure 11

CanUserResizeRow and CanUserResizeColumns

By default, the DataGrid allow users to resize its columns and rows. But you can restrict users if they can resize the DataGrid columns and rows.

The CanUserResizeRows property is used to set whether or not users can resize the row. The CanUserResizeColumns property is used to set whether or not users can resize the columns.

The code snippet in Listing 20 restricts users from resizing rows and columns of the DataGrid.
  1. <DataGrid HorizontalAlignment="Left" Margin="4,5,0,0" VerticalAlignment="Top"   
  2.             Height="120" Width="510" Name="McDataGrid" AutoGenerateColumns="True"  
  3.             AlternatingRowBackground="LightGreen" GridLinesVisibility="Horizontal"  
  4.             ColumnWidth="*" SelectionUnit="Cell" SelectionMode="Extended"  
  5.             CanUserResizeColumns="False" CanUserResizeRows="False"/>  
Listing 20

RowStyle

The RowStyle property is used to style all rows of a DataGrid. The code snippet in Listing 21 creates a Style and sets the DataGrid.RowStyle property.

 

  1. <DataGrid Margin="0,0,0,0" VerticalAlignment="Top"   
  2.     Height="227" HorizontalAlignment="Stretch"   
  3.     HorizontalContentAlignment="Stretch"   
  4.     ColumnWidth="*" Name="McDataGrid"   
  5. >  
  6.     <DataGrid.RowStyle>  
  7.         <Style TargetType="DataGridRow">  
  8.             <Setter Property="Background" Value="LightGreen" />  
  9.             <Style.Triggers>  
  10.                 <Trigger Property="IsMouseOver" Value="True">  
  11.                     <Setter Property="Background" Value="Green"/>  
  12.                     <Setter Property="Foreground" Value="White"/>  
  13.                 </Trigger>  
  14.             </Style.Triggers>  
  15.         </Style>  
  16.     </DataGrid.RowStyle>  
  17.   
  18. </DataGrid>  
Listing 21

Summary

In this article of the Mastering WPF DataGrid series, we saw some of the DataGrid rows related properties.

In the next article of this series, I will cover the basics of the DataGrid columns and their formatting.
 
<<Previous Article                                                  Next Article>> 

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now