This is a migrated thread and some comments may be shown as answers.

Row number column with grouping

7 Answers 231 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Matteo
Top achievements
Rank 2
Matteo asked on 06 Jul 2015, 03:04 PM

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

7 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 08 Jul 2015, 10:11 AM
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
0
Matteo
Top achievements
Rank 2
answered on 20 Jul 2015, 10:41 AM

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

0
Dimitrina
Telerik team
answered on 20 Jul 2015, 12:53 PM
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
0
Matteo
Top achievements
Rank 2
answered on 21 Jul 2015, 02:14 PM

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

0
Dimitrina
Telerik team
answered on 22 Jul 2015, 08:45 AM
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
0
Matteo
Top achievements
Rank 2
answered on 23 Jul 2015, 01:55 PM
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
0
Dimitrina
Telerik team
answered on 23 Jul 2015, 01:58 PM
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
Tags
GridView
Asked by
Matteo
Top achievements
Rank 2
Answers by
Dimitrina
Telerik team
Matteo
Top achievements
Rank 2
Share this question
or