Thursday, November 26, 2009

Calculating Percentage(%) in the ASP.Net Grid View Footer

Yet another scenario, calculating the percentage based on an common property. Say your Stored Procedure returns the ales and Expenses data pinned together, ORDER BY a particular field. Have a look at the below Grid view control. My stored procedure was returning only the Planned-Sales and Planned Expenses for each Division, for each year. M1, M2 M3...M12 represents the Months.
The data which the Stored Procedure returns will be a repeated with Planned-Sales, Planned-Expense for each Sales Division.
Bind your ASP>Net grid view control with data. Rest to be taken care at the Row Data Bound Event of the grid view control.
private void BindMainGrid()
{
string vBdgYear = "2010";
string vSalesDivision = "1";
string vYtdOrMtd = "YTD";

if (DrpBudgetYear.SelectedValue == "0")
{ vBdgYear = null; }
else { vBdgYear = DrpBudgetYear.SelectedValue; }
List oEntity = new List();
DashboardAnalyticsLogic oLogic = new DashboardAnalyticsLogic();
oEntity = oLogic.DashboardAnalytics(vBdgYear, vSalesDivision, vYtdOrMtd);

if (oEntity.Count != 0)
{
GrvMain.DataSource = oEntity;
GrvMain.DataBind();
}
else
{
LblMsg.Text = "No Record found";
}
}
Declare some public variables to calculate the percentage of Expenses against Sales. Go through the code snippet below, First IF condition checks for each data row, you can access each cell item values here with its index mentioned. Obviously, you would be knowing the number of coulmns of the grid view after bind. (I didnt get a chance where the same to be done with dynamicall colum growing grid ) . The second IF finds the footer row and puts data into each cells after the calculation, which would be available from the public variable.
See, first IF condition takes you in when it is a data row with data. (Header row and Footer rows cant get into this loop.)
System.Int64 TotalM1, TotalM2, TotalM3, TotalM4, TotalM5, TotalM6;
protected void GrvMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (TotalM1 == 0) { TotalM1 = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M1")); }
else { TotalM1 = (Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M1")) * 100) / TotalM1; }

if (TotalM2 == 0) { TotalM2 = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M2")); }
else { TotalM2 = (Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M2")) * 100) / TotalM2; }

if (TotalM3 == 0) { TotalM3 = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M3")); }
else { TotalM3 = (Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M3")) * 100) / TotalM3; }

if (TotalM4 == 0) { TotalM4 = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M4")); }
else { TotalM4 = (Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M4")) * 100) / TotalM4; }

if (TotalM5 == 0) { TotalM5 = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M5")); }
else { TotalM5 = (Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M5")) * 100) / TotalM5; }

if (TotalM6 == 0) { TotalM6 = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M6")); }
else { TotalM6 = (Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M6")) * 100) / TotalM6; }
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "Percentage";
e.Row.Cells[4].Text = Convert.ToString(TotalM1) + "%";
e.Row.Cells[5].Text = Convert.ToString(TotalM2) + "%";
e.Row.Cells[6].Text = Convert.ToString(TotalM3) + "%";
e.Row.Cells[7].Text = Convert.ToString(TotalM4) + "%";
e.Row.Cells[8].Text = Convert.ToString(TotalM5) + "%";
e.Row.Cells[9].Text = Convert.ToString(TotalM6) + "%";
e.Row.Cells[1].HorizontalAlign = e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
e.Row.Font.Bold = true;
e.Row.Height = 28;
}
Let me tell you what i did in the Row Data Bound. You agree that the public variables would have value zero at the first time check from the IF condition? If

2 comments:

AK Sabin said...

Wanted Table inside Grid View Row? Just Before The FOOTER?

decimal orderTotal = 0.0m;
decimal tax = 0.0m;
protected void gvOrderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{
//TODO: Calculate the orderTotal and the tax when RowType = DataRow
if (e.Row.RowType == DataControlRowType.Footer)
{
//Create the Row
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
//Add the two Columns
row.Cells.AddRange(CreateCells());
//get a reference to the table that holds this row
Table tbl = (e.Row.Parent as Table);
//Add the row at the end of the list, but before the footer.
tbl.Rows.AddAt(gvOrderDetail.Rows.Count + 1, row);

//Don't forget to account for any changes in the footer. Since we added a row to show the tax,
//that tax must also be accounted for in our footer. Calculating the orderTotal and the tax
//is an exercise for the reader.
Label lbl;
lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = String.Format("{0:C}", (orderTotal + tax));
}
}

private TableCell[] CreateCells()
{

TableCell[] cells = new TableCell[2];
TableCell cell;
Label lbl;

//The order item column
cell = new TableCell();
lbl = new Label();
lbl.Text = "Sales Tax";
cell.Controls.Add(lbl);
cells[0] = cell;

//The price column
cell = new TableCell();
lbl = new Label();
lbl.Font.Bold = true;
lbl.Text = tax.ToString("C");
cell.HorizontalAlign = HorizontalAlign.Right;
cell.Controls.Add(lbl);
cells[1] = cell;

return cells;
}

Anonymous said...

.NET GridView Calculator