I am a beginner in c#.
I am doing a scoring system in c# and there is column name "score".
How do i sum up my values from my "score" column in gridview and get the total score(text)?
Hope you can help me, urgent. Thanks.
Regards,
Angel
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Micke BlomqvistRank #1,660 · 923 reputationPosted Feb 15, 2011, 11:36 PM EST
Just use the property Count of the datatable to devide the total sum with the rows in the table, that will give you the average:
this.lblYourLableDisplayingTheAverageValue.Text = (sum_of_score / dt.Rows.Count).ToString()
Micke BlomqvistRank #1,660 · 923 reputationPosted Feb 12, 2011, 11:18 PM EST
You can loop thru the the rows in the datatable to sum it up:
int sum_of_score = 0;
foreach (DataRow dr in dtScores_table.Rows)
{
sum_of_score += Int32.Parse(dr["scores"].ToString());
}
Now holds the variable sum_of_score the total score.
Angel Park21 reputationPosted Feb 13, 2011, 1:47 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Feb 13, 2011, 5:17 AM EST
You need to provide more information about your thread.
1. How you populating the GridView?
If from DB table, then why create an sql statement for sum values.
2. Or , you entring values in GridView and to show the data in another column?
Micke BlomqvistRank #1,660 · 923 reputationPosted Feb 13, 2011, 8:53 AM EST
Could this short example be useful?
Your aspx page:
<asp:GridView ID="gvScores" runat="server" AutoGenerateColumns="true" ondatabound="gvScores_DataBound" ShowFooter="true">
asp:GridView>
In your aspx.cs file:
protected void gvScores_DataBound(object sender, EventArgs e)
{
try
{
DataTable dt = (DataTable)gvScores.DataSource;
int sum_of_score = 0;
foreach (DataRow dr in dt.Rows)
{
sum_of_score += Int32.Parse(dr["score"].ToString());
}
gvScores.FooterRow.Cells[1].Text = sum_of_score.ToString();
}
catch (Exception rror) { Response.Write(rror.Message); }
}
This assumes your column holding the scores is named score. Just make sure that the ShowFooter is set to true and that you have the correct column name in the sum function and that you select which cell in the footer you want to hold the result.
(gvScores.FooterRow.Cells[!!!!!].Text)
Okay?
Angel Park21 reputationPosted Feb 15, 2011, 1:40 AM EST
Angel Park21 reputationPosted Feb 15, 2011, 1:43 AM EST
Micke BlomqvistRank #1,660 · 923 reputationPosted Feb 15, 2011, 5:44 AM EST
Glad to assist you. To display the total sum value in some other place than in the footer, just use the variable sum_of_score to assign the value to a Label or a TextBox Text property, like this:
this.txtSumOfTotalScores.Text = sum_of_score.ToString(); or this.lblSumOfTotalScores.Text = sum_of_score.ToString();
Where the txtSumOfTotalScores or lblOfTotalScores should be replaced with the name of your control.
Angel Park21 reputationPosted Feb 15, 2011, 5:34 PM EST
Micke BlomqvistRank #1,660 · 923 reputationPosted Feb 15, 2011, 7:22 PM EST
There should not any problem, the DataBound event is happening in the aspx page and you can use the data there no matter where your data comes from or how you have choosen to design your application.
Angel Park21 reputationPosted Feb 15, 2011, 7:55 PM EST
Angel Park21 reputationPosted Feb 15, 2011, 9:50 PM EST
Angel Park21 reputationPosted Feb 15, 2011, 10:56 PM EST
Micke BlomqvistRank #1,660 · 923 reputationPosted Feb 16, 2011, 12:30 AM EST
Angel Park21 reputationPosted Feb 16, 2011, 1:55 AM EST