Sreedhar Danturthi
Sreedhar Danturthi

Reputation: 7571

sorting gridview in asp.net/c#

I have a gridview which is defined as follows:

<asp:GridView ID="ComponentGridView" runat="server" AutoGenerateColumns="true" OnPageIndexChanging="ComponentGridView_PageIndexChanging" OnSorting="ComponentGridView_Sorting">
</asp:GridView>

and these are the methods that get invoked on ComponentGridView_Sorting

    protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = ComponentGridView.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);

            ComponentGridView.DataSource = dataView;
            ComponentGridView.DataBind();
        }
    }

and

 private string ConvertSortDirection(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

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

        return newSortDirection;
    }

But it seems like nothing is happening when i click on the column headers.

What am i doing wrong ?

Please help me

Thanks in anticipation

When i debugged the dataTable is null

DataTable dataTable = ComponentGridView.DataSource as DataTable;

Upvotes: 0

Views: 10429

Answers (2)

Bill
Bill

Reputation: 21

I've been reading and the sorting seems to break when you are manually providing the gridview a datasource. Not sure if that is your case, but this works for me..

When you first load the page and have queried the database, you must save the datatable in a Session variable (only the first time the page is loaded)

Session["GridData"] = myDBTier.GetData();
GridView1.DataSource = Session["GridData"];
GridView1.DataBind();

Then, in the Sorting event for the Gridview, place the following..

string strSortExpression = e.SortExpression + " ASC";
if (Convert.ToString(ViewState["SortExpression"]) == strSortExpression)
{
   strSortExpression = e.SortExpression + " DESC";
}
ViewState["SortExpression"] = strSortExpression;

//This is done by sorting the Default View of the underlying data and then re-binding this //to the grid.

System.Data.DataTable myData = HttpContext.Current.Session["GridData"] as System.Data.DataTable;
if (myData != null)
{
  myData.DefaultView.Sort = strSortExpression;
  GridView1.DataSource = myData;
  GridView1.DataBind();
}

Upvotes: 0

mservidio
mservidio

Reputation: 13057

Keep in mind that by default, this is stateless. So you'll need to retain a reference to your datasource, or query once again for the data.

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("col1");
        dt.Columns.Add("col2");

        dt.Rows.Add(new object[] { 5, 1 });
        dt.Rows.Add(new object[] { 4, 2 });
        dt.Rows.Add(new object[] { 3, 3 });
        dt.Rows.Add(new object[] { 2, 4 });
        dt.Rows.Add(new object[] { 1, 5 });

        this.ComponentGridView.DataSource = dt;
        this.ComponentGridView.DataBind();

        Session["data"] = dt;
    }
}

protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dataTable = Session["data"] as DataTable;

    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);

        ComponentGridView.DataSource = dataView;
        ComponentGridView.DataBind();
    }
}

private string ConvertSortDirection(SortDirection sortDirection)
{
    string newSortDirection = String.Empty;

    switch (sortDirection)
    {
        case SortDirection.Ascending:
            newSortDirection = "ASC";
            break;

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

    return newSortDirection;
}

Upvotes: 4

Related Questions