Friday, April 22, 2011

decimal Total = 0M;
int totalItems = 0;
decimal percent = 0M;
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label markLabel = (Label)e.Row.FindControl("markLabel");
int mark = Convert.ToInt32(markLabel.Text);
Total += mark;
totalItems
+= 1;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label totalLabel = (Label)e.Row.FindControl("totalLabel");
totalLabel
.Text = Total.ToString();
}
}
protected void GridView2_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView2.Rows)
{
Label markLabel = (Label)row.FindControl("markLabel");
int mark = Convert.ToInt32(markLabel.Text);
percent
= (mark / Total) * 100;
Label percentLabel = (Label)row.FindControl("percentLabel");

if (percent == decimal.Truncate(percent)) // no decimal places, is a whole percent
{
percentLabel
.Text = percent.ToString("N0") + "%";
}
else if (percent * 10 == decimal.Truncate(percent * 10)) // dealing with a one decimal place value
{
percentLabel
.Text = percent.ToString("N1") + "%";
}
else if (percent * 100 == decimal.Truncate(percent * 100)) // two decimal place value
{
percentLabel
.Text = percent.ToString("N2") + "%";
}
else
{
percentLabel
.Text = percent.ToString() + "%"; // this is the 3 decimal place representation
}
}
}

0 Comments:

Post a Comment