How to display the row numbers (as sequence) in the Telerik data grid?

1 Answer 75 Views
GridView
Barani
Top achievements
Rank 1
Iron
Iron
Barani asked on 23 Jun 2022, 02:47 PM
I want to implement row numbers in the Telerik data grid. Is there any predefined functions to enable? Or do we have any other method to achieve this. Please guide me how to achieve the above process.

Thanks in advance.

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 27 Jun 2022, 10:55 AM

Hello Barani,

The desired result can be achieved by creating a custom column as demonstrated in the Row Number demo from our WPF Controls Examples application.

Here is the code of interest:

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

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

            textBlock.Text = string.Format("{0}", this.DataControl.Items.IndexOf(dataItem) + 1);

            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();
                        }
                    };
                }
            }
        }
    }

Please let me know if this provides the desired result.

Regards,
Dilyan Traykov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Barani
Top achievements
Rank 1
Iron
Iron
commented on 28 Jun 2022, 04:57 AM

Hello Dilyan,

Thanks for your response. I've made some changes in the above code and got the expected result.
Tags
GridView
Asked by
Barani
Top achievements
Rank 1
Iron
Iron
Answers by
Dilyan Traykov
Telerik team
Share this question
or