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

how to create row number column and pagination

4 Answers 781 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sara
Top achievements
Rank 1
Sara asked on 23 Dec 2019, 09:04 AM

hello

I am trying to implement a Grid View with row number column and pagination.That is, the row number can also be changed when the page is changed

but the code I wrote is that the row number column on each page is only 1 to 10. And by going to the next page the row number starts again from 1 and does not increase to 11 and more.

this is my cs code for row number:

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

and xaml code:

<telerik:RadDataPager DisplayMode="all" PageSize="10" Source="{Binding Items, ElementName=RadGridView}"  />

also i need a label that represents the total number of Grid View rows. The value of this label must be updated by applying a filter or searching.

but my code always represents total number of Grid View rows and does not change with filtering.

 

Thanks!

Sara.

4 Answers, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 26 Dec 2019, 08:02 AM

Hello Sara,

Thank you for the provided code snippet.

The Items collection property represents the current filtered/sorted items in the RadGridView. To get the row number from the collection you can use the ItemsSource instead. For example:

public class CustomGridViewColumn : GridViewDataColumn
{
    public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
    {
        TextBlock textBlock = cell.Content as TextBlock;

        if (textBlock == null)
        {
            textBlock = new TextBlock();
        }
        textBlock.Text = string.Format("{0}", (this.DataControl.ItemsSource as ObservableCollection<MyItem>).IndexOf(dataItem as MyItem) + 1);
        return textBlock;
    }
}

Regarding the total number of gridview rows in filtering and sorting while using RadDataPager. You can create a custom TextBlock and bind its text property to the Items.ItemCount property of the RadGridView.

I have prepared a sample project which demonstrates the above approaches. You can find the project attached to this reply.

Regards,
Dinko
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Sara
Top achievements
Rank 1
answered on 26 Dec 2019, 10:37 AM

Thank's very much, for Response and the sample project.

In sample project, the row number is working correctly.
But when applying the filter or searching, it doesn't work correctly. I want to the row number start at 1 and increase in sequence when applying the row filter.

And also the TextBlock that shows the total number of gridview rows, does not change when the filter is applied. and always is 100.

I want this TextBlock to show the number of rows that are displayed as a result of applying a filter

Thanks,

Sara

 

0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 26 Dec 2019, 02:40 PM

Hello Sara,

Thank you for the provided image.

You are right that after filtering, the row number on a different page is not correct. What you could try is to bind the RadGridView to the PagedSource property of the RadDataPager and the RadDataPager to Data collection. In the CreateCellElement() method, you can set the index depending on the PageIndex property. You can check the modified version of my project attached to this reply.

Regards,
Dinko
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Sara
Top achievements
Rank 1
answered on 28 Dec 2019, 07:07 AM

Thank Dinko for your solution.

it works for me :)

Sara.

Tags
GridView
Asked by
Sara
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Sara
Top achievements
Rank 1
Share this question
or