如何在 asp.net gridview c# 中获取当前分页页面中的行

how to get rows in the current pagination page in asp.net gridview c#

我正在使用 asp.net gridview 和 pagination/sorting,效果很好。 我在网格外有一个按钮说 'Approve current page' 这是只获取当前页面中的行并发送到数据库进行处理。

BindGridView(){
//this will set the initial 
var firstPage = _clientList.Take(xgvClientApprovals.PageSize).ToList();
}
OnApproveClicked(){
    //this will get rows count in each paginated page but I am unable to get 
    //the data itself.
    var currentPageRows = xgvClientApprovals.Rows;
    //Here I would like to get only the current page rows.
}

您可以在 GridView 中使用 DataKeyNames 以始终获取正确的数据。

<asp:GridView ID="GridView1" runat="server" DataKeyNames="approvalID">

然后只需从行中读取正确的 DataKey

protected void Button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        int approvalID = Convert.ToInt32(GridView1.DataKeys[i].Values[0]);
    }
}