编辑Gridview时出错 [英] Error while editing Gridview

查看:62
本文介绍了编辑Gridview时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我可以在第一页编辑gridview,如果我去第二页或除第一页之外的任何其他页面..我无法编辑,得到以下错误。



指数超出范围。必须是非负数且小于集合的大小。参数名称:index



我收到以下行中的错误。

GridViewRow row = GridView1.Rows [index - 1];



完整代码

if(e.CommandName ==edit)

{

PAL.PAL objp = new PAL.PAL();

BAL.BAL objb = new BAL.BAL();

int index = Convert.ToInt32 (e.CommandArgument);

ViewState [id] =索引;

GridViewRow row = GridView1.Rows [index - 1];

Label fname =(Label)row.FindControl(lblFirstname);

Label lname =(Label)row.FindControl(lblLastname);

Label age = (标签)row.FindControl(lblage);

txtFirstname.Text = fname.Text;

txtLastname.Text = lname.Text;

txtage.Text = age.Text;

btn_Add.Text =更新;

formDisplay();

}

Hi,

I can edit the gridview in first page, if I go to 2nd page or any other page apart from first page.. I am unable to edit, getting the following error.

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

I am getting the error in the following line.
GridViewRow row = GridView1.Rows[index - 1];

Full code
if (e.CommandName == "edit")
{
PAL.PAL objp = new PAL.PAL();
BAL.BAL objb = new BAL.BAL();
int index = Convert.ToInt32(e.CommandArgument);
ViewState["id"] = index;
GridViewRow row = GridView1.Rows[index - 1];
Label fname = (Label)row.FindControl("lblFirstname");
Label lname = (Label)row.FindControl("lblLastname");
Label age = (Label)row.FindControl("lblage");
txtFirstname.Text = fname.Text;
txtLastname.Text = lname.Text;
txtage.Text = age.Text;
btn_Add.Text = "Update";
formDisplay();
}

推荐答案

datakeys集合本身可能不为null,但它可以包含零元素。在此示例中,可能是您的网格未设置DataKeyNames属性,因此此网格不会跟踪任何数据键(集合将为空)。这可能是你得到索引错误的原因。


你应该设置DataKeyNames属性。在您的代码中,您还需要检查以确保集合包含元素。
The datakeys collection itself may not be null, but it can contain zero elements. In this example, may be your grid doesn''t set the DataKeyNames property, so this grid isn''t tracking any datakeys (the collection will be empty). That is probably why you are getting an index error.

You should set the DataKeyNames property. In your code you also need to check to make sure the collection contains elements.


这篇关于编辑Gridview时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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