Sorting GridView Formed With Data Set

99封情书 提交于 2020-03-25 08:40:07

问题


The following code sample is for sorting a GridView formed With a DataSet.

Source: http://www.highoncoding.com/Articles/176_Sorting_GridView_Manually_.aspx

But it is not displaying any output. There is no problem in sql connection. I am unable to trace the error, please help me. Thank You.

public partial class _Default : System.Web.UI.Page 
{

    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";

    private DataSet GetData()
    {
         SqlConnection cnn = new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;");
         SqlDataAdapter da = new SqlDataAdapter("SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES", cnn);
         DataSet ds = new DataSet();
         da.Fill(ds);
         return ds;
    }

    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING);
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING);
        }
    }

    private void SortGridView(string sortExpression, string direction)
    {
        // You can cache the DataTable for improving performance
        DataTable dt = GetData().Tables[0];
        DataView dv = new DataView(dt);
        dv.Sort = sortExpression + direction;
        GridView1.DataSource = dv;
        GridView1.DataBind();
    }
}

aspx page

asp:GridView  ID="GridView1"  runat="server"  AllowSorting="True"  OnSorting="GridView1_Sorting">

/asp:GridView>

回答1:


Problem is with Page Load event when you fill the data you add it inside if(!IsPostBack) condition .




回答2:


In this article, there is explained how to sort the GridView data in ASP.NET. GridvIew control is a powerful data grid control that allows us to display the data in tabular format with sorting and pagination. It also allows us to manipulate the data as well.

http://www.dotnetfunda.com/articles/article1598-how-to-sort-the-gridview-data-in-aspnet.aspx




回答3:


    //Code Behind
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
            {

                if (!IsPostBack)
    {
    gvbind();
    }
}

    protected DataSet gvbind()  // Binding the gridview using Dataset and I am using stored procedure "Proc_Displayinfo"
            {
                con.Open();
                SqlCommand command = new SqlCommand("Proc_Displayinfo", con);
                SqlDataAdapter adpt = new SqlDataAdapter(command);
                DataSet ds = new DataSet();
                adpt.Fill(ds);
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
                con.Close();
                return ds;

            }



    public SortDirection dir
        {
            get
            {
                if (ViewState["dirState"] == null)
                {
                    ViewState["dirState"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["dirState"];
            } 
            set
            {
                ViewState["dirState"] = value;
            }

        }


           protected void Gridview1_Sorting(object sender, GridViewSortEventArgs e)
            {


                gvbind();
                DataTable dt = gvbind().Tables[0];

            {
                string SortDir = string.Empty;
                if (dir == SortDirection.Ascending)
                {
                    dir = SortDirection.Descending;
                    SortDir = "Desc";
                }
                else
                {
                    dir = SortDirection.Ascending;
                    SortDir = "Asc";
                }
                DataView sortedView = new DataView(dt);
                sortedView.Sort = e.SortExpression + " " + SortDir;
                GridView1.DataSource = sortedView;
                GridView1.DataBind();
            }
         }

    // Source Code

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="StudentId" onsorting="Gridview1_Sorting" AllowSorting="True">

    <asp:TemplateField HeaderText="StudentID" SortExpression="StudentID">
                                <ItemTemplate>
                                    <asp:Label ID="LBLStudentID" runat="server" Text='<%# Eval("StudentID") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="TXTStudentID" runat="server" Text='<%# Eval("StudentID") %>'></asp:TextBox>
                                </EditItemTemplate>


来源:https://stackoverflow.com/questions/2627234/sorting-gridview-formed-with-data-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!