Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting in gridview is not working

I am trying Sorting functionality in Grid view but its not working.Can some body help?

Code:

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

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

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

    return newSortDirection;
}
protected DataSet FillDataSet()
{
    string source = "Database=GridTest;Server=Localhost;Trusted_Connection=yes";
    con = new SqlConnection(source);
    cmd = new SqlCommand("proc_mygrid", con);
    ds = new DataSet();
    da = new SqlDataAdapter(cmd);
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();

    return ds;


}
 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dt = GridView1.DataSource as DataTable;
    if (dt != null)
    {
        DataView dv = new DataView(dt);
        dv.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
        GridView1.DataSource = dv;
        GridView1.DataBind();
   }

Here dt is coming null.why? pls help thanks.

EDIT:

enter code here  <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
    GridLines="None" AllowPaging="true" AllowSorting="true" PageSize="12" 
    onpageindexchanging="GridView1_PageIndexChanging" 
    onsorting="GridView1_Sorting">

EDIT(Total code)

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con;
    SqlCommand cmd;
    DataSet ds;
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

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

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

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

        return newSortDirection;
    }
    protected DataSet FillDataSet()
    {
        string source = "Database=GridTest;Server=Localhost;Trusted_Connection=yes";
        con = new SqlConnection(source);
        cmd = new SqlCommand("proc_mygrid", con);
        ds = new DataSet();
        da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

        return ds;


    }
    protected void GetValues(object sender, EventArgs e)
    {
        FillDataSet();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       int newPagenumber = e.NewPageIndex;
       GridView1.PageIndex = newPagenumber;
       GridView1.DataSource = FillDataSet();
       GridView1.DataBind();

    }


    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

        DataSet ds = FillDataSet();
        DataTable dt = ds.Tables[0];
        if (dt != null)
        {
            dt.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
like image 889
Wondering Avatar asked Aug 08 '09 11:08

Wondering


1 Answers

CODE:

private string GetSortDirection(string column)
{
       string sortDirection = "DESC";
       string sortExpression = ViewState["SortExpression"] as string;

       if (sortExpression != null)
       {    
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "DESC"))
                {
                    sortDirection = "ASC";
                }
            }
       }

       ViewState["SortDirection"] = sortDirection;
       ViewState["SortExpression"] = column;

       return sortDirection;
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
   DataTable dt = ((DataSet)Session["myDataSet"]).Tables[0];
   dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
   GridView1.DataSource = dt;
   GridView1.DataBind();
}
like image 163
Wondering Avatar answered Oct 23 '22 14:10

Wondering