April 13, 2015 12:17 by
Peter
In this tutorial, I will show you how to Displaying SubTotal & Grand Total in ASP.NET 5. First, create new project. The records are isolated into Groups and after that SubTotal is calculated for every Group and then shown utilizing an element Row as a part of GridView. Now , I write the following code to create Products table:
CREATE TABLE [dbo].[Products](
[ProductID] [int] NULL,
[ProductName] [varchar](100) NULL,
[CategoryID] [int] NULL,
[UnitPrice] [decimal](18, 0) NULL,
[QuantityPerUnit] [varchar](100) NULL
) ON [PRIMARY]
GO
First create new web apps and open your GridViewSubTotalTotal.aspx and write the following code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> Displaying SubTotal & Grand Total in ASP.NET 5</title>
</head>
<body>
<form id="form1" runat="server">
<h3 style="color:Green">Display SubTotal and Grand Total in ASP.Net GridView</h3>
<div>
<asp:GridView ID="gvData" runat="server" BackColor="White" BorderColor="#CC9966"
AutoGenerateColumns="false" BorderStyle="Solid" BorderWidth="1px" CellPadding="4"
Font-Names="Tahoma" Font-Size="Small" Width="475px" OnRowCreated="gvData_RowCreated"
nDataBound="gvData_OnDataBound">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" />
<asp:BoundField DataField="CategoryID" HeaderText="Category ID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" />
<asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:N2}"/>
</Columns>
<FooterStyle BackColor="Tan" />
<AlternatingRowStyle BackColor="#E6E6E1" />
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
</asp:GridView>
</div>
</form>
</body>
</html>
Next step, write the code below: GridViewSubTotalTotal.aspx.cs:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
public partial class GridViewSubTotalTotal : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridData();
}
}
protected void BindGridData()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
string sqlQuery = "SELECT ProductID,ProductName,CategoryID,(UnitPrice*QuantityPerUnit) AS Price FROM Products";
sqlQuery = sqlQuery + " WHERE CategoryID in(1,2,3) ORDER BY ProductID ASC";
SqlCommand cmd = new SqlCommand(sqlQuery, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvData.DataSource = ds;
gvData.DataBind();
}
int currentId = 0;
decimal subTotal = 0;
decimal total = 0;
int subTotalRowIndex = 0;
protected void gvData_RowCreated(object sender, GridViewRowEventArgs e)
{
subTotal = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
int ProductID = Convert.ToInt32(dt.Rows[e.Row.RowIndex]["ProductID"]);
total += Convert.ToDecimal(dt.Rows[e.Row.RowIndex]["Price"]);
if (ProductID != currentId)
{
if (e.Row.RowIndex > 0)
{
for (int i = subTotalRowIndex; i < e.Row.RowIndex; i++)
{
subTotal += Convert.ToDecimal(gvData.Rows[i].Cells[3].Text);
}
this.AddTotalRow("Sub Total", subTotal.ToString("N2"));
subTotalRowIndex = e.Row.RowIndex;
}
currentId = ProductID;
}
}
}
private void AddTotalRow(string labelText, string value)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
row.BackColor = ColorTranslator.FromHtml("#FFA500");
row.Cells.AddRange(new TableCell[4] {new TableCell { Text = labelText, HorizontalAlign = HorizontalAlign.Right},
new TableCell (),
new TableCell(), //Empty Cell,
new TableCell { Text = value, HorizontalAlign = HorizontalAlign.Right }
);
row.Cells[0].BorderColor = System.Drawing.Color.Orange;
row.Cells[1].BorderColor = System.Drawing.Color.Orange;
row.Cells[2].BorderColor = System.Drawing.Color.Orange;
row.Cells[3].BorderColor = System.Drawing.Color.Orange;
gvData.Controls[0].Controls.Add(row);
}
protected void gvData_OnDataBound(object sender, EventArgs e)
{
for (int i = subTotalRowIndex; i < gvData.Rows.Count; i++)
{
subTotal += Convert.ToDecimal(gvData.Rows[i].Cells[3].Text);
}
this.AddTotalRow("Sub Total", subTotal.ToString("N2"));
this.AddTotalRow("Total", total.ToString("N2"));
}
}
I hope this tutorial works for you!
Free ASP.NET 5 Hosting
Try our Free ASP.NET 5 Hosting today and your account will be setup soon! You can also take advantage of our Windows & ASP.NET Hosting support with Unlimited Domain, Unlimited Bandwidth, Unlimited Disk Space, etc. You will not be charged a cent for trying our service for the next 3 days. Once your trial period is complete, you decide whether you'd like to continue.