tags:

views:

33

answers:

2

I had gridview where bind datasource I had to add sorting for this gridview I added the code below to that but It didnot work well.

private string ConvertSortDirectionToSql(SortDirection sortDireciton)
{
    string m_SortDirection = String.Empty;

    switch (sortDireciton)
    {
        case SortDirection.Ascending:
            m_SortDirection = "ASC";
            break;

        case SortDirection.Descending:
            m_SortDirection = "DESC";
            break;
    }

    return m_SortDirection;
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable m_DataTable = GridView1.DataSource as DataTable;

    if (m_DataTable != null)
    {
        DataView m_DataView = new DataView(m_DataTable);
        m_DataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

        GridView1.DataSource = m_DataView;
        GridView1.DataBind();
    }
}
A: 

Maybe this could help if your sortdirection is always ascending.

Tim Schmelter
A: 

you can use this, as I had the same problem and I solve it like this.

public string SortingExpression
{
    get
    {
        if (this.ViewState["SortExpression"] == null)
            return "";
        else
            return (string)this.ViewState["SortExpression"];
    }

    set
    {
        this.ViewState["SortExpression"] = value;
    }
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable m_DataTable = GridView1.DataSource as DataTable;

    if (m_DataTable != null)
    {
        DataView m_DataView = new DataView(m_DataTable);
        SortingExpression = e.SortExpression + " " + (SortingExpression.Contains("ASC") ? "DESC" : "ASC");
        m_DataView.Sort =SortingExpression;

        GridView1.DataSource = m_DataView;
        GridView1.DataBind();
    }
}
Azhar
It didnot work But when I made breakpoint on GridView1_Sorting .I had null data in table .and this error apear (DataTable must be set prior to using DataView) when I hashed if (m_DataTable != null)
KareemSaad