2015-06-16 48 views
-3

我從數據庫中gridviewtextbox檢索數據的大小在數據庫中的兩個行,但不能取,錯誤:索引超出範圍。必須爲非負且小於集合的

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++; 
      } 

我收到錯誤

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

+0

你的排是否有9格?你研究了什麼來解決你的問題? – Sayse

+0

您的視圖是否比行數多一行?第0行是什麼?什麼是在網格視圖中創建行? –

+0

是每行都有9個單元 – Sadhana

回答

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邏輯

所以你的for循環將會從0開始的循環是這樣的

for (int i = 0; i < dt1.Rows.Count; i++) 
       { 
    /.. 
    } 
+0

爲什麼會從一個開始? –

+0

我認爲這是從他的邏輯:) –

+0

@daveL - 操作參考'dt1.Rows [i-1]' – Sayse

相關問題