0

See the below code, I am try to sorting Grid view data by calling function provided by the Microsoft vs 2008, but the paging is done well but the sorting process is not work, tell me where should i change the following code and yes i put grid view in updated penal,is there problem regarding to update penal?

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    showData();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    try
    {
        SqlCommand cmd = new SqlCommand("showData", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0] != null)
        {

            ds.Tables[0].DefaultView.Sort = e.SortExpression + " " + sortType(e.SortDirection);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
    catch (Exception ex)
    {
        Label1.Text = ex.ToString();
    }
}


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

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

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

    return newSortDirection;
}
Dadu
  • 39
  • 1
  • 7

3 Answers3

0

Can you ensure AllowSorting="true" and OnSorting="GridView1_Sorting"

Also refer

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx

http://www.codeproject.com/Articles/26882/Gridview-Sorting-with-Up-and-Down-Icons-Paging

sorting and paging with gridview asp.net

http://www.c-sharpcorner.com/uploadfile/afenster/gridview-sorting/

Hope this helps

Community
  • 1
  • 1
Deepu Madhusoodanan
  • 1,495
  • 1
  • 14
  • 26
0

i think you have to assign dataview to the DataSource property of gridview, not the dataset object. like this

 DataTable dt = ds.Tables[0];

 DataView dv = new DataView(dt); 
 dv.Sort = e.SortExpression + " " + sortType(e.SortDirection);
 GridView1.DataSource = dv;
 GridView1.DataBind();
Talha
  • 18,898
  • 8
  • 49
  • 66
-1

you are missing a default value for newSortDirection.

private string sortType(SortDirection sortDirection)
{
    string newSortDirection = "ASC";

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

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

    return newSortDirection;
}
Ray Cheng
  • 12,230
  • 14
  • 74
  • 137