0

我有一个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>
4

1 回答 1

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
    }
}
于 2012-08-29T18:23:19.033 回答