Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my asp page i have one gridview. It's header field are
(StudentName, Mark1, Mark2,Mark3, Total) but in database i had
(StudentName, Mark1, Mark2,Mark3).


This is my current gridview output
StudentName   Mark1   Mark2   Mark3 Total
Michel         10      11      50
Steven         39      50      35
Deepak         15      18      55


I Need output like below in my asp.net gridview
StudentName   Mark1   Mark2   Mark3 Total
Michel         10      11      50    71
Steven         35      50      35    120
Deepak         15      18      55    88


please some one help me........
Posted
Comments
sathish4303 30-Jan-13 1:46am    
show example plz...

write the sql query while fetching the records sum up or in grid view row data bound you can sum up and display.
 
Share this answer
 
You need to do it in SQL itself instead of writing the server side code and increasing the overhead

SQL
Select Mark1,Mark2,Mark3 , (Mark1+Mark2+Mark3) Total FROM StudentMsrksTable
 
Share this answer
 
Comments
[no name] 30-Jan-13 2:26am    
You should be using SUM() not (Mark1+Mark2+Mark3).
bbirajdar 30-Jan-13 5:58am    
Are you sure? Please show me how? As far as I am working in SQL since the last 8 years, SUM is used for for addition of values in the same column...and not in multiple columns.... What say? Anybody else with the same opinion ????????????

Need a refresher for your basics? Click here and dont forget to change your vote...
Pete O'Hanlon 30-Jan-13 9:12am    
I've countered his univote. You are correct in your assumption.
fjdiewornncalwe 30-Jan-13 10:07am    
My 5 as well.
bbirajdar 30-Jan-13 10:22am    
Thank you Pete and Marc
Find the below code ...it will help u achieve ur task
C#
for (int i=0;i<gridview1.rows.count;i++)>
{
 int totalmasrks=0;
 for(int j=1;j<gridview1.columns.count-1;j++)>
 {
   totalmarks += Convert.ToInt32(GridView1.Rows[i].Cells[j].Text)
 }
 GridView1.Rows[i].Cells[4].Text= tottalmarks.toString();
}



hav great day
Enjoy :)
plz vote and click accept ans if it helps.
 
Share this answer
 
v2
Comments
sathish4303 30-Jan-13 2:06am    
Thanks.....!
fjdiewornncalwe 30-Jan-13 10:06am    
My 1. Very inefficient way to do this. Solution 1 is the way to go.
[no name] 30-Jan-13 11:05am    
my ans is not efficient i knw.. bt its according to need of the person who posted ques.
what the use of givin such a complicated ans which nt evn understandable to person who is at beginner level.
Pete O'Hanlon 30-Jan-13 12:36pm    
A simple, efficient, and 100% correct solution is posted as solution 3. the way to think of it is - you want to perform a set based calculation, but your approach is procedural, so you should use a set based solution. SQL operates as a set based system, so use that where you can.

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