Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Sorting a datagrid

Hi all,

This is my first post and my first attempt to .net.  I'm wondering if anyone can help me out.  I have a datagrid that sorts (by clicking on the column name) and I can edit that datagrid as well.  I have the default sort to sort by EmployeeID.  My problem is when I click on some other column to sort ie. LastName and then I hit the "Edit" button, the sort order goes back to the default sort ie.  EmployeeID.

My code looks somewhat like this:

private string currentSort = "EmployeeID";

protected void AdminGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
            
    currentSort = e.SortExpression;
  LoadDataSet();
}


protected void AdminGrid_Edit(object sender, DataGridCommandEventArgs e)
{
  AdminGrid.EditItemIndex = (int)e.Item.ItemIndex;
  LoadDataSet();
}

private void LoadDataSet()
{
....
dvEmployee = new DataView(dsEmployee.Tables["Table"]);
dvEmployee.Sort = currentSort;
...
}

I searched around on Google and I found some samples using the Session object.  I changed my currentSort to a Session object and it worked.

Any ideas?

Miss Panda
Monday, November 10, 2003

The problem is that fields such as currentSort aren't automatically preserved between requests.  Essentially, a new instance of your page class is initialized for each request. 

ASP.NET provides a variety of methods for preserving data between requests.  The popular ones are Session, Cache, and ViewState.  There are advantages and disadvantages to each though I'd expect to see ViewState used in this case. 

Personally, I like to fall back from the ASP.NET abstraction in cases like this and carry the sort parameter in the QueryString, for example "default.aspx?sort=emailaddr". 

SomeBody
Monday, November 10, 2003

Thanks!

Miss Panda
Wednesday, November 12, 2003

*  Recent Topics

*  Fog Creek Home