我有一个gridview,当视图中有超过10个项目时,会添加一个新页面,非常简单......无论如何,当用户在第二页上并单击该记录上的删除时,该项目来自该行的第一页被删除。示例:第1页有10个项目,第一行是部件号1234,第二行第一行有部件号7890.如果用户单击7890记录的删除,则从第一行删除1234记录,或者他们选择要删除的相应行。
以下是代码:
protected void griditems_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
griditems.PageIndex = e.NewPageIndex;
BindData();
}
编辑对不起大家......这是删除命令:
protected void griditems_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
DataTable dt = (DataTable)Session["table"];
if (dt.Rows.Count > 0)
{
dt.Rows.RemoveAt(e.RowIndex);
griditems.DataSource = dt;
BindData();
}
}
catch
{
//error message
}
}
在aspx中:
<asp:GridView ID="griditems" runat="server" onrowdeleting="griditems_RowDeleting" onrowediting="griditems_RowEditing" onrowupdating="griditems_RowUpdating"
AllowPaging="True" PageSize="10" onpageindexchanging="griditems_PageIndexChanging" Onrowcancelingedit="griditems_RowCancelingEdit"
Caption="Order Details" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" onrowdatabound="griditems_RowDataBound" >
<EditRowStyle BackColor="#FF9900" BorderStyle="Double"/>
<RowStyle HorizontalAlign="Center"/>
</asp:GridView>
答案 0 :(得分:1)
您也应该显示griditems_RowDeleting
实现,但即使没有它,我也可以假设您只需要获取行的索引并将页面编号乘以每页中的记录数量。
Console.WriteLine(e.RowIndex); // 7
Console.WriteLine(griditems.PageIndex); // 3
// Assuming you have 10 records per page, you can safely say...
var realIndexOfRecord = e.RowIndex + griditems.PageIndex * 10; // 37
修改后:
protected void griditems_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
DataTable dt = (DataTable)Session["table"];
if (dt.Rows.Count > 0)
{
// Replace `10` with the appropriate variable
dt.Rows.RemoveAt(e.RowIndex + griditems.PageIndex * 10);
griditems.DataSource = dt;
BindData();
}
}
catch
{
//error message
}
}