0

我想将行中单元格的值增加 1,我该怎么做?尝试了以下方法:

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

3 回答 3

1

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

于 2012-06-22T09:33:52.613 回答
1

.Value 是对象类型,所以 ++ 或 += 不起作用。

尝试:

int value = (int)dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[2].Value;
dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[2].Value = value + 1;
于 2012-06-22T09:36:45.353 回答
0

您不能添加 as Valuefor datagridviewis 类型的对象。所以你必须使用解析值int.TryParse(所以如果单元格包含像文本这样的无效值,解析将失败,你可以根据你的要求更改行为),然后将其添加 1 并将其分配回单元格.

你也可以使用dataGridViewX1.CurrentRow.Cells[2]

于 2012-06-22T09:40:22.167 回答