2012-06-22 242 views
0

我想將行中單元格的值增加1,我該怎麼做?已經嘗試了以下內容:DataGridView單元格值遞增

dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[2].Value =+ 1; 
+1

你將獲得價值分析,然後通過1增加它,'Value'將返回對象 – V4Vendetta

回答

0

不能添加爲Valuedatagridview是對象類型。所以你將不得不使用int.TryParse解析這個值(所以如果單元格包含一個無效的值,比如文本,解析會失敗,你可以根據你的需求改變行爲),然後給它加1,並把它賦值給細胞。

此外,您還可以使用dataGridViewX1.CurrentRow.Cells[2]

1

(int)(dataGridViewX1[row_index][column_index].Value) += 1

+0

我懷疑這是要編譯 – V4Vendetta

1

。價值是對象類型,所以++或+ =將不起作用。

嘗試:

int value = (int)dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[2].Value; 
dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[2].Value = value + 1; 
+0

收到以下錯誤:指定的轉換無效。 – aurelio