指数超出范围。 [英] Index was out of range.
问题描述
嘿所有
当我尝试选择命令时,我的代码中出现以下错误
错误
指数超出范围。必须是非负数且小于集合的大小。参数名称:index
CODE
hey all
when i try to select command the following error comes in my code
ERROR
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
CODE
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
DepartmentSupervisorInfo.DeleteDeptSupervisor(Convert.ToInt32(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text));
GridView1.DataSource = DepartmentSupervisorInfo.getDeptSupervisor();
GridView1.DataBind();
}
else if (e.CommandName == "Select")
{
DataSet ds = DepartmentSupervisorInfo.getDeptSupervisorbyID(Convert.ToInt32(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text));
Hdnfld_deptsupid.Value = ds.Tables[0].Rows[0]["DepartmentSupervisorID"].ToString();
Dd_supDepid.DataValueField = ds.Tables[0].Rows[0]["DepartmentID"].ToString();
dd_supEmpid.DataValueField = ds.Tables[0].Rows[0]["EmployeeID"].ToString();
}
}
此行中的错误
ERROR IN THIS LINE
DataSet ds = DepartmentSupervisorInfo.getDeptSupervisorbyID(Convert.ToInt32(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text));
推荐答案
问题出在这一行
Problem is at this line
GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text
检查GridView1.Rows是否包含那么多行。它似乎没有。例如,Convert.ToInt32(e.CommandArgument)返回2,但GridView1不包含两行。
检查Convert.ToInt32的值(e.CommandArgument)并修复它
Check that GridView1.Rows contains that many rows. It seems that it does not. For example Convert.ToInt32(e.CommandArgument) is returning 2 but the GridView1 does not contain two rows.
Check the value of Convert.ToInt32(e.CommandArgument) and fix it
MessageBox.Show(e.CommandArgument);
//row number is:e.CommandArgument+1
//cell number is:3
MessageBox.Show(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text);
调试前面的代码并将结果粘贴给我。
debug the previous code and paste the result to me .
这篇关于指数超出范围。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!