【问题标题】:Why does sorting does not work when I go to another paging?为什么当我转到另一个分页时排序不起作用?
【发布时间】:2021-10-25 12:21:11
【问题描述】:

在 asp.net 还是新手。我想知道为什么当我更改为另一个分页时排序不起作用?我在第 1 页排序,但是当我转到第 2 页时,排序似乎不起作用。

我想要的是当我单击页眉时,它会一直排序到最后一页,并且在更改分页时不会中断。

此代码有效,但是当我更改分页时,排序中断或不工作。我仍然看到应该在第 1 页而不是在第 2 页的数据。

tblaccount是一个asp:gridview,Sorting and Paging设置为True。

 protected void Page_Load(object sender, EventArgs e)
        {
            //populateCDA();

            if (!Page.IsPostBack)
            {
                refreshdata();
            }

        }

        private void populateCDA()
        {
            DataTable table = new DataTable();
            // get the connection    
            using (SqlConnection conn = new SqlConnection(mc.defaultConnection))
            {
                // write the sql statement to execute    
                string sql = "SELECT * FROM [dbo].[GETACCOUNTS]()";
                // instantiate the command object to fire    
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    // get the adapter object and attach the command object to it    
                    using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                    {
                        // fire Fill method to fetch the data and fill into DataTable    
                        ad.Fill(table);
                    }
                }
            }
            // specify the data source for the GridView    
            tblViewAccount.DataSource = table;
            // bind the data now    
            tblViewAccount.DataBind();
        }

        public void refreshdata()
        {
            
            SqlConnection con = new SqlConnection(mc.defaultConnection);
            SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[GETACCOUNTS]()", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            tblViewAccount.DataSource = dt;
            tblViewAccount.DataBind();
            ViewState["dirState"] = dt;
            ViewState["sortdr"] = "Asc";


        }

        protected void tblViewAccount_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dtrslt = (DataTable)ViewState["dirState"];
            if (dtrslt.Rows.Count > 0)
            {
                if (Convert.ToString(ViewState["sortdr"]) == "Asc")
                {
                    dtrslt.DefaultView.Sort = e.SortExpression + " Desc";
                    ViewState["sortdr"] = "Desc";
                }
                else
                {
                    dtrslt.DefaultView.Sort = e.SortExpression + " Asc";
                    ViewState["sortdr"] = "Asc";
                }
                tblViewAccount.DataSource = dtrslt;
                tblViewAccount.DataBind();


            }
        }

        protected void tblViewAccount_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            tblViewAccount.PageIndex = e.NewPageIndex;
            tblViewAccount.DataBind();
        }

【问题讨论】:

  • 分页发生在排序之前。所以,如果你有 1 5 4 2 3 6 并且页面大小是 3,你会得到第 1 页:1 4 5 和第 2 页:2 3 6。虽然你期望 1 2 3 和 4 5 6,对吧?
  • 是的。但是我得到第 1 页:6 5 4 和第 2 页:4 5 6,排序停止或仅使用上面的代码在当前页面上工作。单击当前页面中的标题对当前页面进行排序,而不是对页面的其余部分进行排序。
  • 好吧,这很奇怪......那么它不仅仅是步骤的顺序。
  • 我希望它不仅仅是单个页面或第 1 页,而是页面的其余部分。
  • 向我们解释dtrslt.DefaultView.Sort = e.SortExpression + " Desc"; 试图实现的目标。如果您在该行上放置一个断点,它会在反转排序顺序时被命中吗?切换页面时会被击中吗? stackoverflow.com/questions/12643474/… 对您的上下文有帮助吗?

标签: c# asp.net pagination


【解决方案1】:

当您分页时 - 您必须将网格重新绑定到表格。

所以试试这个:

DataTable dtrslt = (DataTable)ViewState["dirState"];
tblViewAccount.PageIndex = e.NewPageIndex;
tblViewAccount.DataSource = dtrslt;    
tblViewAccount.DataBind();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多