views:

685

answers:

2

So I'm writing a page which does some reporting and it's pretty dynamic, the user has the ability to group data and sort columns. I've been able to get my dynamic grouping down and then the sorting except now my generated linq-to-sql sql order by statement is backwards from what I want it to be. I think I need to figure out how to get at the Result returned from the LinqDataSource when the Sorting event is fired in the ListView so that I can then append my grouping order by clauses.

Currently I'm overriding the LinqDataSource selecting event to provide it's result and do the dynamic grouping there, but then the Result is modified by the Sort on the listview and I was thinking there must be some way to get at that result during the Sorting or Sorted event.

Any insight would be appreciated, in the meantime I will be attempting to figure this out myself.

+1  A: 

Awesome I think I found my answer, set AutoSort to false on my LinqDataSource and then sort the data myself, since I'm already sorting myself for the other data.

Dave
+1  A: 

You can use Linq to manipulate the results of your queries (IEnumerables) based on user inputs. For example (very pseudocode event handler follows):

public void OnUserClickNameColumn(object sender, EventArgs e){
  var data = DataProvider.RetrieveAllUsers();
  // switch between sorting asc and desc when user clicks on a column head
  SessionHelper.CurrentUser.NameColumnSort = !SessionHelper.CurrentUser.NameColumnSort;
  if(SessionHelper.CurrentUser.NameColumnSort) // true =  sort asc else sort desc
    UserDataGrid.DataSource = data.OrderBy(x=>x.Name);
  else
    UserDataGrid.DataSource = data.OrderByDescending(y=>y.Name);
}
Will