[英]Error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection
[英]Error: Index was out of range. Must be non-negative and less than the size of the collection
我正在將數據從數據庫檢索到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++)
{
/..
}
請注意,如果您的 bussniss 邏輯,您應該從 0 開始循環
所以你的 for 循環會是這樣的
for (int i = 0; i < dt1.Rows.Count; i++)
{
/..
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.