DataGrid: Defining Row Header?

Nov 12, 2008 at 2:55 PM
Edited Nov 12, 2008 at 2:58 PM


I tried the whole day to display the RowHeaders in a DataGrid, but did not succeed by now.

I've got a DataTable bound to a DataGrid. Showing ColumnHeaders is no problem, also the RowHeaders are displayed by setting property, but how can I get text in there?!

Here is the example code:


public Window1()
DataTable dt = new DataTable();
for (int icol = 0; icol <= 4; icol++)
DataColumn dc = new DataColumn(icol.ToString());

for (int i = 0; i <= 4; i++)
DataRow dr = dt.NewRow();

for (int irow = 0; irow <= 4; irow++)

dr[irow] = i.ToString() + "." + irow.ToString();

dataGrid1.DataContext = dt.DefaultView;  

And the XML:

<Window x:Class="WpfApplication3.Window1"
Title="Window1" Height="300" Width="300" xmlns:my="">

<my:DataGrid ItemsSource="{Binding}" Canvas.Left="99" Canvas.Top="126" Height="Auto" Name="dataGrid1" Width="Auto" HeadersVisibility="All" BorderBrush="LightBlue" HorizontalGridLinesBrush="LightBlue" VerticalGridLinesBrush="LightBlue" EnableColumnVirtualization="False" SelectionMode="Single" SelectionUnit="Cell" EnableRowVirtualization="False" />

Thanks for your help!



Nov 12, 2008 at 6:34 PM
Hi Chris,

You can put the following Style in the Resources of your xaml file to add text to your Row Headers.  In the example below, we've got the text bound to the ID field of the data.  You'll need to change this to bind to your own field or another property.

<Style x:Key="RowHeadersWithId" TargetType="{x:Type dg:DataGridRowHeader}">
<Setter Property="Content" Value="{Binding Id}"/>


Nov 13, 2008 at 7:32 AM
Hey Sam, thank you so much! It worked!
Jun 18, 2009 at 12:48 PM

Hi, I have one question how to align the content to center of the RowHeader,
here is my code:
    <Style x:Key="ControlRowHeaders" TargetType="{x:Type dg:DataGridRowHeader}">
        <Setter Property="Content" Value="{Binding Path=RowHeaderImage}"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>

Thanks very much