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

How to select required grid page in code behind?

5 Answers 205 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Evgeny
Top achievements
Rank 1
Evgeny asked on 30 Jun 2008, 04:50 PM
Hello,

Background: I have toolbar, paged grid, and the form (actually user control) to edit records outside the grid. Usual flows are:
1. User clicks on grid row, I fill the form with the selected row data, user see it. He can then edit data and click Save button on toolbar. Data has been saved and grid has been rebinded then.
2. User clicks Add button on the toolbar, I clear the form out. User enters data and clicks Save button on the toolbar. Data has been saved and grid has been rebinded again.

After the grid has been rebinded, I must select the row (which was just edited or added) programmatically using Grid.SelectedIndexes.Add... (first I determine required page index and row index on page of course).

Everything works great unless changed or added row appears within currently selected grid page after rebinding (e.g data is sorted alphabetically in database query, no sorting in the grid itself, and new record may or may not appear in the currently selected grid page after rebind).

What I can: determine required page index from the underlying dataset (which is possible only when there is no sorting in grid abd is just a workaround that I don't like very much, but no other way found in your help/forums, but it's a subject for another discussion).

I.e. I have all information in my code required to set the corresponding page index. The final step left: select required page (knowing it's index) in the server code behind.

I tried to set Grid.CurrentPageIndex in PreRender event handler as well as just after data reload - nothing works for me. It seems that content to render inside the grid has been created before PreRender event so setting CurrentPageIndex doesn't change anything - after request, I see the old page selected despite of the index I'm setting.

Any way to get what I need (i.e. set selected page of paged grid in code behind and see THAT grid page in browser)?

Please note that switching grid paging off is not an acceptable approach for me: having hundreds or thousands records in dataset, it takes too much time to load page when everything is shwon at time.

Or if I rephrase the question: how to select a row in paged grid, which is located on a page which is not currently selected page in the grid, programmatically?

5 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 01 Jul 2008, 12:48 PM
Hello Evgeny,

You can check this article to know more about how to use selection on paging, sorting, etc:
http://www.telerik.com/help/aspnet-ajax/grdpersistselectedrowsonsorting.html

Sincerely yours,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Evgeny
Top achievements
Rank 1
answered on 01 Jul 2008, 01:37 PM
Hello Vlad,

Thank you for the link, it's a very useful article but unfortunatelly it doesn't make any help with my problem. My issue is the problem of selecting the given grid page in code behind. Let's say the current page number is 1, and user click a button on page which causes inserting data record into the underlying data table. Let's say this inserted row will appear in the page number 3 of the grid. How can I make this page number 3 selected then? After that, it's not a problem for me to find and select the required row.
0
Sebastian
Telerik team
answered on 04 Jul 2008, 08:16 AM
Hello Evgeny,

Generally, the grid will navigate automatically to the last page when insert operation is performed and you should be able to select the newly added grid record on that page.

Additionally, if you want to navigate to the last page explicitly when other operation occurs, consider setting the CurrentPageIndex property of the grid to the last index available and refresh the control invoking its Rebind() method.

Best regards,
Stephen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Evgeny
Top achievements
Rank 1
answered on 04 Jul 2008, 10:57 AM
Hello,

Thanks for the advice. Last page is not a correct point in my case because the data in grid is sorted and the last inserted record will not be the last row in grid and will not be located in the last page. I need a way to select the certain page that I need, not just a first or last page.

I will check about invoking Rebind method and report if it works, thanks
0
Evgeny
Top achievements
Rank 1
answered on 05 Jul 2008, 11:39 AM
Finally, this flow worked for me in PreRender event handler:

RadGrid1.CurrentPageIndex = pageIndex;

RadGrid1.Rebind();

RadGrid1.SelectedIndexes.Add(indexToSelect);

here pageIndex is index of the Page I need to select, indexToSelect is the index of the row I need to select on that page.

Thanks to everybody for your answers!
Tags
Grid
Asked by
Evgeny
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Evgeny
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or