如何在GridView中获取第二页的行索引? [英] how to get row index of second page in gridview?

查看:113
本文介绍了如何在GridView中获取第二页的行索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有gridview总共6条记录...我将页面大小保持为3 ....意味着每页我将获得3条记录....现在我必须在gridview中删除第二页中的记录. .因为我必须在行命令事件中捕获行索引...请参阅下面的代码,如果我尝试删除gridview第一页中的记录,则会使索引超出范围错误.请帮助我...

here i have gridview with total 6 records ...i kept page size as 3....means for every page i will get 3 records....now i have to delete the record in second page in gridview..for tht i have to capture row index in row command event...see my below code iam getting index out of range error if i try to delete record in first page in gridview ..pls help me out...

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {        
            if (e.CommandName == "ImgAppDelete")
            {               
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = GridView1.Rows[index];
                DataTable dt = new DataTable();
                dt = (DataTable)ViewState["dt"];                              
                dt.Rows.RemoveAt(row.RowIndex + GridView1.PageSize);
                dt.AcceptChanges();
                GridView1.DataSource = dt;
                GridView1.DataBind();              
            }
}

推荐答案

获取索引超出范围错误的原因是由于使用索引删除行时索引中的更改所致commandArgument值.这是执行此操作的一种方法,但是工作量太大,因为您还需要针对数据行管理索引. .Net已经内置了此功能,请按照以下步骤进行操作.

解决方案是将数据行键字段值绑定到行命令对象,即
The reason you are getting the index out of bound error is due to the changes in the indexes when you are deleting the row using the index commandArgument value. This is one way to do it, but is too much work as you also need to manage the indexes against the rows of data. .Net already has this built in and is done as follows.

The solution is to bind the Data Row key field value to the row command object i.e.
CommandArgument='<%#Eval("IdColumnName")%>'



将其放在删除/删除"按钮/控件上.

单击后,可以从e.commandArgument
解析值
然后,在rowCommand-delete部分中,删除该行,如下所示:

首先找到数据行:



Put that on the delete/remove button/control.

When it is clicked, you can parse the value from e.commandArgument

Then, in the rowCommand - delete part, delete the row like this:

first find the data row:

DataRow[] drArray = dt.Select("KeyFieldName = " + e.commandargument.toString());
if(drArray.Length == 1) //To avoid object instance not reference error
{
  DataRow dr = drArray[0];
  dr.Delete();
  dt.AcceptChanges();
  GridView1.DataSource = dt;
  GridView1.DataBind(); 
}





您可以使用记录的唯一标识符在gridview中插入/更新/删除行.该唯一标识符是数据键".


查看以下链接以获取示例

How_to_edit_and_Delete_records_in_a_gridview_using_datadigs [
Hi,


You can use a unique identifier of your record to insert/update/delete your rows in gridview.This unique identifier is the "datakey".


Check out the below link to get the sample

How_to_edit_and_Delete_records_in_a_gridview_using_datakeys[^]


Hope this helps.


这篇关于如何在GridView中获取第二页的行索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆