Row number column with grouping

8 posts, 0 answers
  1. Matteo
    Matteo avatar
    8 posts
    Member since:
    May 2012

    Posted 06 Jul 2015 Link to this post

    Hi,

    following the demo I've created an attached property to my RadGridView that adds a column that displays the number of the row.

    Everything works fine, but if my grid has a group descriptor defined in xaml or loaded from the custom settings saved by the user, then the numbers appear not sorted.

    It seems like the numeration happens before the grouping, but I don't know how to fix it.

    Here's my code:

    public class RowNumeratorBehavior
        {
            public static bool GetRowNumerator(RadGridView grid)
            {
                return (bool)grid.GetValue(RowNumeratorProperty);
            }

            public static void SetRowNumerator(RadGridView grid, bool value)
            {
                grid.SetValue(RowNumeratorProperty, value);
            }

            public static readonly DependencyProperty RowNumeratorProperty =
                DependencyProperty.RegisterAttached("RowNumerator", typeof(bool), typeof(RowNumeratorBehavior), new UIPropertyMetadata(OnAutoSelectedChanged));

            static void OnAutoSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                RadGridView source = (sender as RadGridView);
                if (source == null) return;
                if ((bool)e.NewValue)
                {
                    source.DataContextChanged += source_DataContextChanged;
                    Telerik.Windows.Persistence.Services.ServiceProvider.RegisterPersistenceProvider<Telerik.Windows.Persistence.Services.ICustomPropertyProvider>(typeof(RadGridView), new GridViewCustomPropertyProvider());
                }
                else
                {
                    source.DataContextChanged -= source_DataContextChanged;
                }
            }

            private static void source_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                RadGridView source = (sender as RadGridView);

                var numerator = new RowNumberColumn();
                numerator.Header = "";
                numerator.Name = "Numerator";
                numerator.UniqueName = "Numerator";
                numerator.IsReorderable = false;
                numerator.Background = System.Windows.Media.Brushes.White;
                source.Columns.Insert(0,numerator);
            }
        }

     

     public class RowNumberColumn : Telerik.Windows.Controls.GridViewDataColumn
        {        
            public override System.Windows.FrameworkElement CreateCellElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem)
            {
                TextBlock textBlock = cell.Content as TextBlock;

                if (textBlock == null)
                {
                    textBlock = new TextBlock();
                }

                textBlock.Text = ((this.DataControl.ItemsSource as IList).IndexOf(dataItem) + 1).ToString();

                return textBlock;
            }

            protected override void OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
            {
                base.OnPropertyChanged(args);

                if (args.PropertyName == "DataControl")
                {
                    if (this.DataControl != null && this.DataControl.Items != null)
                    {
                        this.DataControl.Items.CollectionChanged += (s, e) =>
                        {
                            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
                            {
                                this.Refresh();
                            }
                        };
                    }
                }
            }
        } 

     

    Thanks in advance,

    Matteo

  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 08 Jul 2015 Link to this post

    Hi Matteo,

    Would you please share how do you define the GroupDescriptor in XAML? You say you are following the demo, do you mean the "Row Number" wpf demo since when I group on it the numbers appear as sorted?

    Regards,
    Dimitrina
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Matteo
    Matteo avatar
    8 posts
    Member since:
    May 2012

    Posted 20 Jul 2015 in reply to Dimitrina Link to this post

    Hi Dimitrina,

    yes I'm following that demo. My code works as well if I group the colums by dragging them in the Group Panel. 

    If I define my groupdescriptors like this:

                    <telerik:RadGridView.GroupDescriptors>
                        <telerik:GroupDescriptor Member="CentroDiLavoro" />
                        <telerik:GroupDescriptor Member="GiornoProduzione" />
                        <telerik:GroupDescriptor Member="Turno" />
                    </telerik:RadGridView.GroupDescriptors>

    inside the telerik:RadGridView, the row number is shown not sorted instead.

    I use group descriptors because I want users to have a default setting that ​they can customize.

    Regards,

    Matteo

  5. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 20 Jul 2015 Link to this post

    Hi Matteo,

    You can apply a ColumnGroupDescriptor instead. Please check the Programmatic Grouping article on how to add such in XAML.

    Regards,
    Dimitrina
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Matteo
    Matteo avatar
    8 posts
    Member since:
    May 2012

    Posted 21 Jul 2015 in reply to Dimitrina Link to this post

    Hi Dimitrina,

    I've applied ColumnGroupDescriptors like this:
    <telerik:RadGridView.GroupDescriptors>
             <telerik:ColumnGroupDescriptor Column="{Binding Columns[CentroDiLavoro], ElementName=ConsuntivoTelerikGrid}" />
             <telerik:ColumnGroupDescriptor Column="{Binding Columns[GiornoProduzione], ElementName=ConsuntivoTelerikGrid}" />
             <telerik:ColumnGroupDescriptor Column="{Binding Columns[Turno], ElementName=ConsuntivoTelerikGrid}" />
    </telerik:RadGridView.GroupDescriptors>

    Grouping works, but row numbers are still unsorted.

    Regards,
    Matteo

  7. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 22 Jul 2015 Link to this post

    Hi Matteo,

    You say it works on the demo which you are following when the user groups manually. Basically when the user drags a column to group on it, a new ColumnGroupDescriptor is added to RadGridView.GroupDescriptiors collection. 
    As adding such in XAML seems to not work the same way, would it be possible for you to isolate the case in a demo project and send it to us in a new support ticket? You can also take a look at this blog post for a reference on how to isolate an issue. 

    That way we will review the specific case and advice further.

    Regards,
    Dimitrina
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  8. Matteo
    Matteo avatar
    8 posts
    Member since:
    May 2012

    Posted 23 Jul 2015 in reply to Dimitrina Link to this post

    Hi Dimitrina,
    while creating the demo project I found the solution to the problem:

    I was also defining a SortDescriptor which I changed into a ColumnSortDescriptor. That didn't work, but I figured out that it was obsolete, I could specify a sort direction in the ColumnGroupDescriptor.
    I removed the ColumnSortDescriptor and added the SortDirection and everything now works fine, the row number is correctly sorted.

    I don't know why the ColumnSortDescriptor was messing the numerator: I tried to manually sort the column (not the group) after loading the grid data and the numeration would then sort as expected.

    Thanks for the support and have a nice day!

    Matteo
  9. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 23 Jul 2015 Link to this post

    Hi Matteo,

    I am glad to hear that! Thank you for sharing the solution you came up with.

    Regards,
    Dimitrina
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WPF is Visual Studio 2017 Ready