【问题标题】:Sorting GridView Formed With Data Set对数据集形成的 GridView 进行排序
【发布时间】:2011-02-07 07:21:41
【问题描述】:

以下代码示例用于对GridViewDataSet 形成的排序。

来源:http://www.highoncoding.com/Articles/176_Sorting_GridView_Manually_.aspx

但它没有显示任何输出。 sql连接没有问题。 我无法追踪错误,请帮助我。 谢谢。

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 页面

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

/asp:GridView>

【问题讨论】:

  • 问题中引用的链接帮助我实现了我想要解决的一个问题,gridview 没有返回任何行。解决方案是将“AutoGenerateColumns”属性设置为 true。这适用于我的情况,因为我不想隐藏任何列。

标签: asp.net c#-4.0 view grid dataset


【解决方案1】:
    //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>

【讨论】:

    【解决方案2】:

    本文介绍了如何在 ASP.NET 中对 GridView 数据进行排序。 GridvIew 控件是一个强大的数据网格控件,它允许我们通过排序和分页以表格格式显示数据。它还允许我们操作数据。

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

    【讨论】:

      【解决方案3】:

      当您填充添加到 if(!IsPostBack) 条件中的数据时,问题在于 Page Load 事件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-07
        • 1970-01-01
        • 2013-06-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多