Link to home
Create AccountLog in
Avatar of whiwex
whiwex

asked on

Gridview SortExpression is empty

I am trying to sort a gridview that I have on a page.
The gridview is bound to a dataset that was created via code.
When I click on a column the sorting event is fired.
In the sort event I check the sortexpresion but it is always empty. I think the sort expression should contain the column heading that was clicked. Is this correct?
Avatar of oleggold
oleggold
Flag of United States of America image

asp.net on sort method:

protected void OnSort(object sender, GridViewSortEventArgs e)
{
    // There seems to be a bug in GridView sorting implementation. Value of
    // SortDirection is always set to "Ascending". Now we will have to play
    // little trick here to switch the direction ourselves.
    if (String.Empty != m_strSortExp)
    {
        if (String.Compare(e.SortExpression, m_strSortExp, true) == 0)
        {
            m_SortDirection = 
                (m_SortDirection == SortDirection.Ascending) ? SortDirection.Descending : SortDirection.Ascending;
        }
    }
    ViewState["_Direction_"] = m_SortDirection;
    ViewState["_SortExp_"] = m_strSortExp = e.SortExpression;
    this.bindGridView();
}							
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        if (String.Empty != m_strSortExp)
        {
            AddSortImage(e.Row);
        }
    }
}
void AddSortImage(GridViewRow headerRow)
{
    Int32 iCol = GetSortColumnIndex(m_strSortExp);
    if (-1 == iCol)
    {
        return;
    }
    // Create the sorting image based on the sort direction.
    Image sortImage = new Image();
    if (SortDirection.Ascending == m_SortDirection)
    {
        sortImage.ImageUrl = "~/dwn.gif";
        sortImage.AlternateText = "Ascending Order";
    }
    else
    {
        sortImage.ImageUrl = "~/up.gif";
        sortImage.AlternateText = "Descending Order";
    }
 
    // Add the image to the appropriate header cell.
    headerRow.Cells[iCol].Controls.Add(sortImage);
}
			

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oleggold
oleggold
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of whiwex
whiwex

ASKER

I create a dataset from a acess database and bind this to the gridview.
Should this work?