We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Adding a Row Number Column to SfDatagrid without adding an additional Index

Hello Everyone,

I currently have a sfdatagrid that holds row information, and I would like to add an additional small column all the way to the right that holds the row information. For example, row 1 would have 1 in the row number, row 5 would have row 5 in the row number, etc. I have tried to set 

dataTableMain.ShowRowHeader = true;

However, I get errors like this - 

System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')'

So I do not think I can use the ShowRowHeader as this adds another index to my sfdatagrid, and most of my program uses these indexes for other functions, which is why the error is thrown. Is there an easier way to add a row number column without adding another column index?


4 Replies

VS Vijayarasan Sivanandham Syncfusion Team February 23, 2023 08:59 PM UTC

Hi Jacob Treisch,

Your requirement of adding a row number in the SfDataGrid can be achieved by customizing the row header cell style.

UG Link: https://help.syncfusion.com/wpf/datagrid/styles-and-templates#displaying-row-index-in-row-header-cell

KB Link: https://www.syncfusion.com/kb/5522/how-to-display-rowindex-at-rowheadercell-in-sfdatagrid

The reported problem occurs due to a mismatched index being passed in your implemented functions. You can resolve this by using the helper methods ResolveToStartColumnIndex and ResolveToGridVisibleColumnIndex. Refer to the code snippet below.

private void OnGetColumnIndexClicked(object sender, RoutedEventArgs e)


    //here get the column by using column Index

    var column = sfDataGrid.Columns[sfDataGrid.ResolveToGridVisibleColumnIndex(3)];


    //Resolves the GridColumn index from the visible column index. It excludes the RowHeader and IndentColumn.

    var startColumnIndex = sfDataGrid.ResolveToStartColumnIndex();


UG Link:

If you still facing the same issue, provide more information related to your query.

        1. Share the code snippet which leads to exceptions in SfDataGrid.

        2. If possible, kindly modify the attached sample based on your scenario.

        3. Video Illustration of the Reported Problem


Kindly revert to us with the requested details above. It will be more helpful for us to check the possibilities of resolving the reported problem.

Vijayarasan S

Attachment: Sample_e9791581.zip

JT Jacob Treisch March 1, 2023 01:46 PM UTC

I have tried adding in the row header grid, but it is still not working. I have attached the XAML code for this sfdatagrid. Can you please let me know where the code should go. 


 <!-- Top / main table -->

                            <GroupBox Grid.Row="3" >

                                <ScrollViewer x:Name="mainTableContainer" Grid.Row="3" PreviewMouseWheel="mainTableContainer_PreviewMouseWheel" Focusable="False">


                                        <syncfusion:SfDataGrid SelectionMode="Single"






                                                   CellStyleSelector="{StaticResource styleSelector}"



                                                   HeaderTemplate="{StaticResource TheHeaderTemplate}"














                                                <Style TargetType="{x:Type syncfusion:GridHeaderCellControl}">

                                                    <Setter Property="Background" Value="{DynamicResource {x:Static adonisUi:Brushes.AccentBrush}}"></Setter>

                                                    <Setter Property="Foreground" Value="{DynamicResource HeaderTextColor}"></Setter>







                            <!-- totals columns for first table -->

                            <GroupBox Grid.Row="4" >

                                <syncfusion:SfDataGrid ScrollViewer.HorizontalScrollBarVisibility="Disabled" Visibility="Visible" SelectionMode="Single" NavigationMode="Cell"

                                           AllowDeleting="True" AddNewRowPosition="None" Grid.Row="4" x:Name="dataTableMainSummary"

                                           CellStyleSelector="{StaticResource summaryStyleSelector}"



                                           Foreground="{DynamicResource {x:Static adonisUi:Brushes.AccentForegroundBrush}}"



                                        <Style TargetType="syncfusion:GridHeaderCellControl">

                                            <Setter Property="Foreground" Value="{DynamicResource {x:Static adonisUi:Brushes.ForegroundBrush}}"/>





JT Jacob Treisch March 1, 2023 07:36 PM UTC

I added the code, and I think I have it close to working. The rows headers show, but when I click on the sfdatagrid, I get this error 

System.NullReferenceException: 'Object reference not set to an instance of an object.'

This exception was originally thrown at this call stack:

    Syncfusion.UI.Xaml.Grid.DataRow.UpdateRenderer(Syncfusion.UI.Xaml.Grid.DataColumnBase, Syncfusion.UI.Xaml.Grid.GridColumn)

    Syncfusion.UI.Xaml.Grid.DataRow.UpdateColumn(Syncfusion.UI.Xaml.Grid.DataColumnBase, int)









VS Vijayarasan Sivanandham Syncfusion Team March 2, 2023 01:45 PM UTC

Jacob Treisch,

We have found the same query in forum thread 180831. This is a duplicate thread for this query. Please follow thread 180831 for further updates. As always, we will be happy to assist you.

Live Chat Icon For mobile
Up arrow icon