Friday, October 5, 2018

Display SubTotal and Grand Total Year wise using C# in ASP.Net

Display SubTotal and Grand Total Year wise using C#  in ASP.Net


HTML

<div>
<asp:GridView ID="gvData" AutoGenerateColumns="false"
OnDataBound="OnDataBound" OnRowCreated="OnRowCreated"
        runat="server">
        <Columns>
       <asp:BoundField HeaderText="District" DataField="District" />
       <asp:BoundField HeaderText="Year" DataField="Year" />
       <asp:BoundField HeaderText="Total Works" DataField="Total Works" />
       <asp:BoundField HeaderText="Completed" DataField="Completed" />
        </Columns>
    </asp:GridView>
</div>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] {
            new DataColumn("District")
        ,new DataColumn("Year")
        ,new DataColumn("Total Works")
        ,new DataColumn("Completed")
        });
        dt.Rows.Add("Delhi", "2013-14", "72", "72");
        dt.Rows.Add("Bihar", "2013-14", "57", "57");
        dt.Rows.Add("UP", "2013-14", "200", "200");
        dt.Rows.Add("Andhra Pradesh", "2013-14", "100", "100");
        dt.Rows.Add("Chandigarh", "2013-14", "82", "77");
        dt.Rows.Add("Goa", "2013-14", "81", "81");
        dt.Rows.Add("Gujrat", "2014-15", "225", "218");
        dt.Rows.Add("Haryana", "2014-15", "162", "156");
        dt.Rows.Add("Kerala", "2014-15", "573", "485");
        dt.Rows.Add("Maharashtra", "2014-15", "261", "249");
        dt.Rows.Add("Ranchi", "2014-15", "173", "107");
        dt.Rows.Add("Tripura", "2014-15", "248", "201");
        gvData.DataSource = dt;
        gvData.DataBind();
    }
}
     
string currentYear = string.Empty;
int subTotal = 0;
int total = 0;
int subTotalRowIndex = 0;
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
    subTotal = 0;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
        string year = dt.Rows[e.Row.RowIndex]["year"].ToString();
        total += Convert.ToInt32(dt.Rows[e.Row.RowIndex]["Total Works"]);
        if (year != currentYear)
        {
            if (e.Row.RowIndex > 0)
            {
                for (int i = subTotalRowIndex; i < e.Row.RowIndex; i++)
                {
                    subTotal += Convert.ToInt32(gvData.Rows[i].Cells[2].Text);
                }
                this.AddTotalRow("Sub Total", subTotal.ToString("N2"));
                subTotalRowIndex = e.Row.RowIndex;
            }
            currentYear = year;
        }
    }
}
private void AddTotalRow(string labelText, string value)
{
    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, 
DataControlRowState.Normal);
    row.BackColor = ColorTranslator.FromHtml("#F9F9F9");
    row.Cells.AddRange(new TableCell[3] { new TableCell (), //Empty Cell
    new TableCell { Text = labelText, HorizontalAlign = HorizontalAlign.Right},
    new TableCell { Text = value, HorizontalAlign = HorizontalAlign.Right } });
    gvData.Controls[0].Controls.Add(row);
}
     
protected void OnDataBound(object sender, EventArgs e)
{
    for (int i = subTotalRowIndex; i < gvData.Rows.Count; i++)
    {
        subTotal += Convert.ToInt32(gvData.Rows[i].Cells[2].Text);
    }
    this.AddTotalRow("Sub Total", subTotal.ToString("N2"));
    this.AddTotalRow("Total", total.ToString("N2"));
}

No comments:

Post a Comment