简体   繁体   中英

DataGridView Cell Value Increment

I would like to increase the value of the cell in the row by 1, how can i do this? 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.

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. 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.

Also you can also use dataGridViewX1.CurrentRow.Cells[2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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