我正在从数据库检索数据到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++;
}
我收到错误
错误:索引超出范围。必须是非负的且小于 集合的大小
答案 0 :(得分:0)
那里有两个概率
1)你的网格没有8个细胞
2)您的for loop
不正确
而不是
for (int i = 1; i <= dt1.Rows.Count; i++)
{
//...
}
试试这个
for (int i = 1; i < dt1.Rows.Count; i++)
{
/..
}
注意,如果你的bussniss逻辑
,你应该用0开始循环所以你的for循环就像这样
for (int i = 0; i < dt1.Rows.Count; i++)
{
/..
}