views:

971

answers:

4

Hi..

I have a gridview (actually a SPgridview)

And i made the columnname clickable so the users can sort the rows using the data. And that works fine.

The problem occurs when users try to select a row after they sorted the data. I can see that the gridview kinda "forgets" how the rows were sorted and selects the row that was at the clicked index before it got sorted..

How do i fix that? I tried sorting the row again after the user selects a row, but that doesnt seem to work. And should the gridview remember the fact that it was just sorted?

Thanks in advance :)

+1  A: 

Make sure that you are not rebinding the grid after a postback.

if(!IsPostBack)
{
  gridView.DataSource = yourDataSource;
  gridView.DataBind();
}
ichiban
Ah sorry, maybe it is worth mentioning that the spgridview is placed inside a sharepoint webpart.So i have to create the controls after every postback. :/
Moulde
+1  A: 

Are you grabbing the selected row by it's row index or by the unique identifier of the data you are wanting to edit? If you're getting by row index, it may be 'forgetting' since you are recreating the Grid on OnPostBack. Try iterating through the data and select it by it's unique ID, not its row index.

Wayne Hartman
Im getting the selectedDatakey, and i connect to another webpart using that key, and that works, but its like the gridview overides my selection with its own or something.Because i run though the rows and set the one that matches the datakey to selected, but after postback it resets the sorting and the row get selected by index instead.
Moulde
A: 

I got it working. (kinda)

In the sorting event i saved the sortexpression (the name of the column used to sort by) and the sortdirection ascending or descending.

Then i make the datasource for the gridview and databind, and after databinding it, i use the gridview.sort command to sort by the values i saved in viewstate.

That works fine, only one problem. When sorting i made it switch direction after pressing the same column more than one time. Now it thinks i keep pressing the column title, so it keeps reversing the sorting.

But i tempererarely made it only sort in one direction. And now im playing with the sender object in the sorting event, im thinking that if i could get some info about whats causing the event i could tell it to only switch direction based on the sender.

Thanks :)

Moulde
+1  A: 

Check Johans blog regarding SPGridView and LinqDataSource

salgo60
Thanks for linking to my blog
Johan Leino