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

问题描述 投票:0回答:1

我使用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.
}
asp.net gridview pagination
1个回答
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]);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.