简体   繁体   English

DataGridView单元格值增量

[英]DataGridView Cell Value Increment

I would like to increase the value of the cell in the row by 1, how can i do this? 我想将行中单元格的值增加1,我该怎么做? Have tried the following: 尝试了以下内容:

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

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

.Value is of type object, so ++ or += won't work. .Value属于object类型,因此++或+ =不起作用。

Try: 尝试:

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

You cannot add as Value for datagridview is of type object. 您不能添加作为Valuedatagridview是对象类型。 so you will have to parse the value using int.TryParse (so in case if the cell contains an invalid value like text the parse would fail and you can change the behavior depending on your requirement) and then add 1 to it and assign it back to the cell. 所以你必须使用int.TryParse来解析这个值(所以如果单元格包含像文本一样的无效值,解析会失败,你可以根据你的要求改变行为),然后加1并将其分配回来到细胞。

Also you can also use dataGridViewX1.CurrentRow.Cells[2] 你也可以使用dataGridViewX1.CurrentRow.Cells[2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM