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?


Thanks!


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:
https://help.syncfusion.com/wpf/datagrid/helpers

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.

Regards,
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. 

Thanks!


 <!-- Top / main table -->

                            <GroupBox Grid.Row="3" >

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

                                    <Grid>

                                        <syncfusion:SfDataGrid SelectionMode="Single"

                                                   NavigationMode="Cell"

                                                   AllowDeleting="True"

                                                   AddNewRowPosition="Bottom"

                                                   x:Name="dataTableMain"

                                                   BorderThickness="0"

                                                   CellStyleSelector="{StaticResource styleSelector}"

                                                   SortColumnsChanging="dataTableMain_SortColumnsChanging"

                                                   SortColumnsChanged="dataTableMain_SortColumnsChanged"

                                                   HeaderTemplate="{StaticResource TheHeaderTemplate}"

                                                   AllowRowHoverHighlighting="True"

                                                   AllowSelectionOnPointerPressed="True"

                                                   AllowDraggingRows="True"

                                                   AllowDrop="True"

                                                   HeaderRowHeight="50"

                                                   SelectionChanged="dataTableMain_SelectionChanged"

                                                   CurrentCellDropDownSelectionChanged="dataTableMain_CurrentCellDropDownSelectionChanged"

                                                   CurrentCellBeginEdit="Spin_OnEdit"

                                                   >

                                        <interactivity:Interaction.Behaviors>

                                            <!--<local:SfDataGridBehavior/>-->

                                        </interactivity:Interaction.Behaviors>



                                        <syncfusion:SfDataGrid.HeaderStyle>



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

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

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




                                                </Style>


                                            </syncfusion:SfDataGrid.HeaderStyle>


                                        </syncfusion:SfDataGrid>

                                    </Grid>

                                </ScrollViewer>

                            </GroupBox>

                            <!-- 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}"

                                           BorderThickness="0"

                                           RowHeight="50"

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


                                           >

                                    <syncfusion:SfDataGrid.HeaderStyle>

                                        <Style TargetType="syncfusion:GridHeaderCellControl">

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


                                        </Style>

                                    </syncfusion:SfDataGrid.HeaderStyle>

                                </syncfusion:SfDataGrid>

                            </GroupBox>






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)

    Syncfusion.UI.Xaml.Grid.DataRow.EnsureColumns(Syncfusion.UI.Xaml.ScrollAxis.VisibleLinesCollection)

    Syncfusion.UI.Xaml.Grid.RowGenerator.EnsureColumns(Syncfusion.UI.Xaml.ScrollAxis.VisibleLinesCollection)

    Syncfusion.UI.Xaml.Grid.VisualContainer.EnsureItems(bool)

    Syncfusion.UI.Xaml.Grid.VisualContainer.MeasureOverride(System.Windows.Size)

    System.Windows.FrameworkElement.MeasureCore(System.Windows.Size)

    System.Windows.UIElement.Measure(System.Windows.Size)

    System.Windows.ContextLayoutManager.UpdateLayout()

    System.Windows.ContextLayoutManager.UpdateLayoutCallback(object)



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.


Loader.
Up arrow icon