2012-02-26 63 views
0

我遇到了一個令人沮喪的錯誤,我確定它很簡單,但我無法弄清楚是什麼。設置datagridview單元格值的索引錯誤

這裏是我的功能:。

public void setDistanceCellValue(int value, int row, int column) 
{ 
    try 
    { 
     Console.WriteLine("Row: " + row + " Column: " + column + " Value: " + value); 
       logGrid.Rows[row].Cells[column].Value = value.ToString(); 
    } 
    catch (Exception ex) 
    { 
      MessageBox.Show(string.Format("An error occurred: {0}" + "\r\n" + "\r\n" + "Row: " + row + " Column: " + column + " Value: " + value, ex.Message)); 
    } 
} 

logGrid值設定線就是與「索引超出範圍示數出必須爲非負數且小於集合的大小參數名稱:索引「。

我一直在檢查和檢查,我的網格有6行8列,並且該函數被稱爲第2行第3列(因此所有的調試代碼)。對於我的生活,我無法弄清楚爲什麼它說我正在使用負指數或e.rowIndex或7對於e.columnIndex大於5的指數。

幫我,如果你可以。

+0

你能告訴我們這個函數被調用的循環嗎? – roymustang86 2012-02-26 21:37:12

回答

1

指數範圍從0到N-1,而不是從1到N


編輯:

兩個建議

  1. 現有Console.WriteLine之前添加額外的調試代碼
Console.WriteLine("Row Count: {0}", logGrid.Rows.Count); 
    Console.WriteLine("Cell Count: {0}", logGrid.Rows[row].Cells.Count); 
  1. 檢查AutoGenerateColumns p看看它是否符合你的預期。
+0

是的,這就是爲什麼對於總共6行,索引從0到5,但我不能解釋爲什麼設置中間單元格的值(如果它只發生在0號或第N個索引)仍然會拋出該錯誤? – ikathegreat 2012-02-26 00:18:22

+0

這個'e.rowIndex'來自哪裏。我的代碼中沒有看到任何'e'。錯誤是否發生在別的地方? – 2012-02-26 00:23:38

+0

,它簡單地來自'private void logGrid_CellMouseDoubleClick(object sender,DataGridViewCellMouseEventArgs e)',以提供被雙擊的單元格的行和列索引。 – ikathegreat 2012-02-26 04:19:06

相關問題