Row numbers do not work correctly: Fixed.

May 13, 2013 at 6:56 PM
Hello, I have found that when scrolling, the row numbers become incorrect. After some research I have found that it has something to do with how the WPF DataGrid is implemented to increase efficiency. I have solved the problem as follows:

In the ExtendedDataGridLoaded event I add the following:
if (ShowRowNumber)
                DataGridBehavior.SetRowNumber(this, true);
I added a DataGridBehavior class in the classes folder of the ExtendedDataGrid project. This class is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using ExtendedGrid.Microsoft.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace ExtendedGrid.Classes
    /// <summary>
    /// Class for displaying the row numbers in the row headers.
    /// </summary>
    public class DataGridBehavior
        public static DependencyProperty DisplayRowNumberProperty =
                                                new FrameworkPropertyMetadata(false, OnRowNumbersChanged));

        /// <summary>
        /// Gets the DisplayRowNumberProperty's value.
        /// </summary>
        /// <param name="target">The target to get the value from</param>
        /// <returns>The value</returns>
        public static bool GetRowNumber(DependencyObject target)
            return (bool)target.GetValue(DisplayRowNumberProperty);
        }//end GetDisplayRowNumber

        /// <summary>
        /// Sets the DisplayRowNumberProperty to value.
        /// </summary>
        /// <param name="target">The target to set the property of</param>
        /// <param name="value">The value to set the property to</param>
        public static void SetRowNumber(DependencyObject target, bool value)
            target.SetValue(DisplayRowNumberProperty, value);
        }//end SetDisplayRowNumber

        /// <summary>
        /// Subscribes or unsubscribes the LoadingRow and UnloadingRow events to the RefreshRowNumbers method
        /// in order to properly update the row numbers when rows are added or deleted.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="args"></param>
        private static void OnRowNumbersChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
            DataGrid grid = source as DataGrid;
            if (grid == null)

            if ((bool)args.NewValue)
                grid.LoadingRow += RefreshRowNumbers;
                grid.UnloadingRow += RefreshRowNumbers;
                grid.LoadingRow -= RefreshRowNumbers;
                grid.UnloadingRow -= RefreshRowNumbers;
        }//end OnRowNumbersChanged

        /// <summary>
        /// Event handler that handles refreshing the row numbers in the row headers.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private static void RefreshRowNumbers(object sender, DataGridRowEventArgs args)
            DataGrid grid = sender as DataGrid;
            if (grid == null)

            foreach (var item in grid.Items)
                var row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(item);
                if (row != null)
                    row.Header = (row.GetIndex() + 1).ToString();
        }//end RefreshRowNumbers

    }//end class DataGridBehavior
}//end namespace
This also accounts for adding and deleting rows as well.
May 20, 2013 at 6:13 PM

Just made 2 small changes.

The grid property EnableRowVirtualization needs to be set to false.

Second change is sometime GetIndex was returning -1 when EnableRowVirtualization is set to true, for safety I kept a check after null check if its not -1.

May 21, 2013 at 9:56 PM
No problem. I forgot to mention a bit of XAML that makes it work with the EnableRowVirtualization:
                    <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:DataGridRow}}, Path=Header}"/>
Where the namespaces are:
Put this inside the <my:ExtendedDataGrid> tags and it should be good to go. Sorry about the omission.