Click here to Skip to main content
15,904,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi i have a data table and grid view. i have assigned data source property to grid view

now i want to calculate sum of the two columns and show it in another column or in label how do i show that????????

in which event i can get these columns values. ????

Thanks in advance
Posted
Updated 17-Jan-10 21:28pm
v2

You can iterate over your data source to find the data you want, and as you do that, you can add a column to your data source and fill it with the data you need. Assuming where the data comes from is a database, it would make more sense to do your calculation and add the column in the DB.
 
Share this answer
 
If you want to do at UI level or BL level, you can create a new datatable as your datasource. For that, define the expected columns from query result and add a new column as sum that you need.

Iterate Row-wise though the datatable you got from DB. Set the values of all the columns row-wise. Lets say, something like:3
foreach (DataRow dr in dt.Rows)
        {
            tempValueIn1stColumn = Convert.ToDouble(dr["1st Column Name"]);
            tempValueIn2ndColumn = Convert.ToDouble(dr["2nd Column Name"]);
            tempValueIn3rdColumn = tempValueIn1stColumn + tempValueIn2ndColumn; 
            // Set the three values in the new table now. 
        }


Though this can be done, but for best performance doing this in database would be advisable.
 
Share this answer
 
In GridView, you can use rowdatabound event, in which you can find the third column label control and set the text from your side.

Check from your side.

Thanks.

Mehul Thakkar
 
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