sum 2 cells in datagridview

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I have a datagridview and would like to get the sum of 2 cells and display the sum in another column....the data in the cells is formatted like so "##,###,###" where the number sign is an actual number.I have written some code to take out the commas and do the math...now I need to figure out how to put the commas back in when the math is complete.....not all the cells will have the same amount of numbers for instance one cell might have "###,###" and others might have "#,###" if that makes any sence?

any help would be appreciated

-InkedGFX
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,136
Location
Sydney, Australia
Programming Experience
10+
You're making this way harder than it needs to be. You don't need to do anything in code to remove commas or put them back in. If you put numbers in the cells to begin with then you just do the maths and that's it. In fact, you don't even need to do the maths yourself. The simple option is to create a DataTable with three columns with a data type of Int32. For the third column, you set its Expression property to the sum of the other two. You then bind that DataTable to the grid. Now, the third column will automatically contain the sum of the other two. You can set the DefaultCellStyle.Format property of each column to format the numbers in the appropriate way so the commas will appear on their own.
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
Im new to using the datagrid and was unaware this was possible...I will look into doing this...thank you for pointing me in the correct direction with this....

thank you
-InkedGFX
 
Top Bottom