Row numbers do not work correctly: Fixed.

May 13, 2013 at 7: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 =
            DependencyProperty.RegisterAttached("DisplayRowNumber",
                                                typeof(bool),
                                                typeof(DataGridBehavior),
                                                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)
                return;

            if ((bool)args.NewValue)
            {
                grid.LoadingRow += RefreshRowNumbers;
                grid.UnloadingRow += RefreshRowNumbers;
            }
            else
            {
                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)
                return;

            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.
Coordinator
May 20, 2013 at 7:13 PM
Thanks,

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.

Regards,
bpoojary
May 21, 2013 at 10:56 PM
No problem. I forgot to mention a bit of XAML that makes it work with the EnableRowVirtualization:
            <my:ExtendedDataGrid.RowHeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:DataGridRow}}, Path=Header}"/>
                </DataTemplate>
            </my:ExtendedDataGrid.RowHeaderTemplate>
Where the namespaces are:
        xmlns:controls="clr-namespace:ExtendedGrid.Microsoft.Windows.Controls;assembly=ExtendedGrid" 
        xmlns:my="clr-namespace:ExtendedGrid.ExtendedGridControl;assembly=ExtendedGrid"
Put this inside the <my:ExtendedDataGrid> tags and it should be good to go. Sorry about the omission.