Click here to Skip to main content
15,888,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem to perform sum on two datagridview cells(as two matrix),just like:
dataGridView5.Rows[2].Cells[i].Value=dataGridView5.Rows[2].Cells[i].Value+dataGridView5.Rows[2].Cells[i-1].Value

the erorr is "operator'+' cannot be applied to operands of type'object'and 'object' "
plz help me to solve this problem.tnx
Posted

try below, convert to int ( I assume you have integer values) and take the sum

C#
dataGridView5.Rows[2].Cells[i].Value=Convert.ToInt32(dataGridView5.Rows[2].Cells[i].Value.ToString())+Convert.ToInt32(dataGridView5.Rows[2].Cells[i-1].Value.ToString())
 
Share this answer
 
v2
Comments
nedas65 3-May-14 2:01am    
tnx.it's worked.
DamithSL 3-May-14 2:05am    
you are welcome :) I'm glad to help you
Convert these values into numeric types (integer or decimal) before doing a sum on these values.
 
Share this answer
 
Use the cell's Text property and convert it to Integer.

dataGridView5.Rows[2].Cells[i].Value=Convert.ToInt32(dataGridView5.Rows[2].Cells[i].Text)+Convert.ToInt32(dataGridView5.Rows[2].Cells[i-1].Text) 
 
Share this answer
 
If you are sure about values are numeric and you are using .Net 4 or higher you can use dynamic.

C#
dataGridView5.Rows[2].Cells[i].Value = (dynamic)dataGridView5.Rows[2].Cells[i].Value +(dynamic)dataGridView5.Rows[2].Cells[i - 1].Value;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900