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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Micke BlomqvistPosted Feb 16, 2011, 4:36 AM
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()
Angel ParkPosted Feb 16, 2011, 6:55 AM
Micke BlomqvistPosted Feb 16, 2011, 5:30 AM
Angel ParkPosted Feb 16, 2011, 3:56 AM
Angel ParkPosted Feb 16, 2011, 2:50 AM
Angel ParkPosted Feb 16, 2011, 12:55 AM
Micke BlomqvistPosted Feb 16, 2011, 12:22 AM
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 ParkPosted Feb 15, 2011, 10:34 PM
Micke BlomqvistPosted Feb 15, 2011, 10:44 AM
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 ParkPosted Feb 15, 2011, 6:43 AM
Angel ParkPosted Feb 15, 2011, 6:40 AM
Micke BlomqvistPosted Feb 13, 2011, 1:53 PM
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?
Suthish NairPosted Feb 13, 2011, 10:17 AM
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?
Angel ParkPosted Feb 13, 2011, 6:47 AM
Micke BlomqvistPosted Feb 13, 2011, 4:18 AM
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.