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:
- Mastering WPF DataGrid in a Day: Hour 1 Introduction
- Mastering WPF DataGrid in a Day: Hour 2 DataGrid Layout and Border
- Mastering WPF DataGrid in a Day: Hour 3 DataGrid Header
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.
- <DataGrid HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"
- Height="219" Width="465" Name="McDataGrid" AutoGenerateColumns="False"
- RowHeaderWidth="50" />
The output looks as in Figure 8.

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.

Listing 16
The code snipet in Listing 17 hides both row and column headers of a DataGrid.
- <DataGrid HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"
- Height="219" Width="509" Name="McDataGrid" AutoGenerateColumns="True"
- RowHeaderWidth="10" AlternatingRowBackground="LightGreen"
- GridLinesVisibility="Horizontal" />
The output looks as in Figure 9.

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.
- <DataGrid HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"
- Height="219" Width="509" Name="McDataGrid" AutoGenerateColumns="True"
- RowHeaderWidth="10" AlternatingRowBackground="LightGreen" />
The output looks as in Figure 10.

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.


Dinesh JoshiPosted Jan 22, 2018, 5:10 PM
After the last article still "Next Article>>" button showing same color, even not disable we can improve this.
mohamad hassanPosted May 26, 2015, 3:46 AM
Thank You