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

时间:2019-02-14 17:55:48

标签: asp.net gridview pagination

我正在使用带有分页/排序功能的asp.net gridview,它工作得很好。 我在网格外部有一个按钮,说“批准当前页面”,该按钮仅获取当前页面中的行并发送到db进行处理。

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.
}

1 个答案:

答案 0 :(得分:1)

您可以在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]);
    }
}