Wednesday, January 11, 2012

sub totals in Gridview

protected void grdByDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = new DataTable();
if (ViewState["dt_Detail"] == null)
{
DataView dv = (DataView)grdProspectDet.DataSource;
dt = dv.ToTable();
DataRow workRow = dt.NewRow();
dt.Clear();

}
else
{
dt = (DataTable)ViewState["dt_Detail"];

}

if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblPremum = ((Label)e.Row.FindControl("lblPremium"));
double Premium = Convert.ToDouble(lblPremum.Text.ToString());
lblPremum.Text = Premium.ToString("C", new System.Globalization.CultureInfo("en-US"));

if (e.Row.RowIndex == 0)
{

Carrier = ((Label)e.Row.FindControl("lblCarrier")).Text.ToString();

CarrierPremium = Convert.ToDouble(((Label)e.Row.FindControl("lblPremium")).Text.TrimStart('$'));
CarrierPremium2 = Convert.ToDouble(((Label)e.Row.FindControl("lblPremium")).Text.TrimStart('$'));

TotalPremium = CarrierPremium2;

DataRow workRow = dt.NewRow();
workRow["ClientID"] = ((Label)e.Row.FindControl("lblClientID")).Text.ToString();
workRow["ClientName"] = ((Label)e.Row.FindControl("lblClientName")).Text.ToString();
workRow["Vendor"] = Carrier.ToString();
workRow["Premium"] = CarrierPremium2.ToString();
workRow["CurrentMonth"] = ((Label)e.Row.FindControl("lblCurrentMonth")).Text.ToString();
workRow["CurrentYear"] = ((Label)e.Row.FindControl("lblCurrentYear")).Text.ToString();
dt.Rows.Add(workRow);
}
else
{

if (Carrier == ((Label)e.Row.FindControl("lblCarrier")).Text)
{

CarrierPremium += Convert.ToDouble(((Label)e.Row.FindControl("lblPremium")).Text.TrimStart('$'));
CarrierPremium2 = Convert.ToDouble(((Label)e.Row.FindControl("lblPremium")).Text.TrimStart('$'));
TotalPremium += CarrierPremium2;

DataRow workRow = dt.NewRow();
workRow["ClientID"] = ((Label)e.Row.FindControl("lblClientID")).Text.ToString();
workRow["ClientName"] = ((Label)e.Row.FindControl("lblClientName")).Text.ToString();
workRow["Vendor"] = Carrier.ToString();
workRow["Premium"] = CarrierPremium2.ToString();
workRow["CurrentMonth"] = ((Label)e.Row.FindControl("lblCurrentMonth")).Text.ToString();
workRow["CurrentYear"] = ((Label)e.Row.FindControl("lblCurrentYear")).Text.ToString();
dt.Rows.Add(workRow);
}
else
{
// Carrier = ((Label)e.Row.FindControl("lblCarrier")).Text.ToString();

DataRow workRow = dt.NewRow();
workRow["Vendor"] = "Total Premium for " + Carrier.ToString();
workRow["Premium"] = CarrierPremium.ToString();
dt.Rows.Add(workRow);

Table tblTemp = (Table)this.grdProspectDet.Controls[0];
int intIndex = tblTemp.Rows.GetRowIndex(e.Row);
GridViewRow gvrSubTotal = CreateGridViewRowByDetail(intIndex,"aliceblue",grdProspectDet.Columns.Count,"" + CarrierPremium.ToString("C", new System.Globalization.CultureInfo("en-US")),
20,"Total Premium for "+""+Carrier+"");
tblTemp.Controls.AddAt(intIndex, gvrSubTotal);
Carrier = ((Label)e.Row.FindControl("lblCarrier")).Text.ToString();

CarrierPremium = Convert.ToDouble(((Label)e.Row.FindControl("lblPremium")).Text.TrimStart('$'));
CarrierPremium2 = Convert.ToDouble(((Label)e.Row.FindControl("lblPremium")).Text.TrimStart('$'));
TotalPremium += CarrierPremium2;

DataRow workRow2 = dt.NewRow();
workRow2["ClientID"] = ((Label)e.Row.FindControl("lblClientID")).Text.ToString();
workRow2["ClientName"] = ((Label)e.Row.FindControl("lblClientName")).Text.ToString();
workRow2["Vendor"] = Carrier.ToString();
workRow2["Premium"] = CarrierPremium2.ToString();
workRow2["CurrentMonth"] = ((Label)e.Row.FindControl("lblCurrentMonth")).Text.ToString();
workRow2["CurrentYear"] = ((Label)e.Row.FindControl("lblCurrentYear")).Text.ToString();
dt.Rows.Add(workRow2);
}
if (iRowsCount == e.Row.RowIndex)
{
DataRow workRow3 = dt.NewRow();
workRow3["Vendor"] = "Total Premium for" + Carrier.ToString();
workRow3["Premium"] = CarrierPremium.ToString();
dt.Rows.Add(workRow3);

Table tblTemp = (Table)this.grdProspectDet.Controls[0];
int intIndex = tblTemp.Rows.GetRowIndex(e.Row) + 1;

GridViewRow gvrLast = CreateGridViewRowByDetail(intIndex, "aliceblue", grdProspectDet.Columns.Count, "" + CarrierPremium.ToString("C", new System.Globalization.CultureInfo("en-US")), 20, "Total Premium for " + "" + Carrier + "");
GridViewRow gvrLast2 = CreateGridViewRowByDetail(intIndex, "cornsilk", grdProspectDet.Columns.Count, "" + TotalPremium.ToString("C", new System.Globalization.CultureInfo("en-US")), 20, "Total Premium");
tblTemp.Controls.AddAt(intIndex, gvrLast);
tblTemp.Controls.AddAt(intIndex + 1, gvrLast2);

DataRow workRow = dt.NewRow();
workRow["Vendor"] = "Total Premium";
workRow["Premium"] = TotalPremium.ToString();
dt.Rows.Add(workRow);



}
}

//if (e.Row.RowType == DataControlRowType.Footer)
//{
// Label lblTotalPremium = (Label)e.Row.FindControl("lblTotalPremium");
// lblTotalPremium.Text = CarrierPremium.ToString("C", new System.Globalization.CultureInfo("en-US"));



//// lblTotalAP.Text = TotalAP.ToString("C", new CultureInfo("en-US"));

//}
}
ViewState["dt_Detail"] = dt;
}

No comments:

Post a Comment