错误:索引超出范围.必须是非负数且小于集合的大小
本文关键字:小于 集合 是非 索引 范围 错误 | 更新日期: 2023-09-27 17:56:32
我正在从数据库中检索数据,gridview
textbox
数据库中有两行但无法获取它:
string sr_no;
int rowIndex = 0;
for (int i = 1; i <= dt1.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox6");
TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox7");
TextBox box8 = (TextBox)Gridview1.Rows[rowIndex].Cells[8].FindControl("TextBox8");
box1.Text = dt1.Rows[i-1]["RD_PS_APPLab_Parameter"].ToString();
sr_no = dt1.Rows[i-1]["RD_SR_No"].ToString();
box2.Text = dt1.Rows[i-1]["Test"].ToString();
box3.Text = dt1.Rows[i-1]["Test_Condition"].ToString();
box4.Text = dt1.Rows[i-1]["Method"].ToString();
box5.Text = dt1.Rows[i-1]["CTQ"].ToString();
box6.Text = dt1.Rows[i-1]["Specification_RD_PS_AppLab"].ToString();
box7.Text = dt1.Rows[i-1]["UOM"].ToString();
box8.Text = dt1.Rows[i-1]["Remarks"].ToString();
rowIndex++;
}
我收到错误
错误:索引超出范围。必须为非负数且小于 集合的大小
那里有拖曳概率
1)您的网格剂量没有8个细胞
2)您的for loop
不正确而不是
for (int i = 1; i <= dt1.Rows.Count; i++)
{
//...
}
试试这个
for (int i = 1; i < dt1.Rows.Count; i++)
{
/..
}
请注意,如果您的总线逻辑,您应该以 0 开始循环
所以你的 for 循环会是这样的
for (int i = 0; i < dt1.Rows.Count; i++)
{
/..
}