[英]Display Total in ASP.Net GridView in C# and show with grouping
This is asp.net code 这是asp.net代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<style>
.SubTotalRowStyle {
border: solid 1px Black;
background-color: #81BEF7;
font-weight: bold;
}
.GrandTotalRowStyle {
border: solid 1px Black;
background-color: Gray;
font-weight: bold;
}
.GroupHeaderStyle {
border: solid 1px Black;
background-color: #81BEF7;
font-weight: bold;
}
.ExpandCollapseStyle {
border: 0px;
cursor: pointer;
}
.ExpandCollapseGrandStyle {
border: 0px;
cursor: pointer;
}
</style>
<link href="Styles/Site.css" rel="stylesheet" />
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdViewOrders" runat="server" AutoGenerateColumns="False" TabIndex="1"
Width="100%" DataSourceID="XmlDataSource1" CssClass="grdViewOrders"
CellPadding="4" ForeColor="Black" GridLines="Vertical"
OnRowDataBound="grdViewOrders_RowDataBound"
OnRowCreated="grdViewOrders_RowCreated" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px">
<Columns>
<asp:TemplateField HeaderText="">
<ItemStyle Width="10px" />
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemStyle Width="10px" />
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="OrderID" HeaderText="OrderID">
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="ProductName" HeaderText="ProductName">
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Quantity" HeaderText="Quantity">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Discount" HeaderText="Discount">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Amount" HeaderText="Amount">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundField>
</Columns>
<RowStyle BackColor="#F7F7DE" BorderStyle="Solid" BorderWidth="1px" BorderColor="Black" />
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" BorderStyle="Solid" BorderWidth="1px" BorderColor="Black" />
<AlternatingRowStyle BackColor="White" BorderStyle="Solid" BorderWidth="1px" BorderColor="Black" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="Data/Orders.xml"></asp:XmlDataSource>
</div>
</form>
<script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
//ExpandCollapse
$(document).ready(function () {
$('.ExpandCollapseStyle').click(function () {
var orderId = $(this).attr('alt');
if (!isDisplayed($('.ExpandCollapse' + orderId))) {
$(this).attr('src', 'images/minus.gif');
$('.ExpandCollapse' + orderId).css("display", "block");
}
else {
$(this).attr('src', 'images/plus.gif');
$('.ExpandCollapse' + orderId).css("display", "none");
}
})
$('.ExpandCollapseGrandStyle').click(function () {
$(".grdViewOrders tr").each(function () {
var orderId = $(this).find(".ExpandCollapseStyle").attr('alt');
if (orderId != 'undefined') {
if ($(this).attr('alt') == 'Expanded') {
$(this).find(".ExpandCollapseStyle").attr('src', 'images/minus.gif');
$('.ExpandCollapse' + orderId).css("display", "block");
$(this).attr('alt', 'Collapsed');
}
else {
$(this).find(".ExpandCollapseStyle").attr('src', 'images/plus.gif');
$('.ExpandCollapse' + orderId).css("display", "none");
$(this).attr('alt', 'Expanded');
}
}
});
if ($('.ExpandCollapseGrandStyle').attr('alt') == 'Expanded') {
$('.ExpandCollapseGrandStyle').attr('src', 'images/plus.gif');
$('.ExpandCollapseGrandStyle').attr('alt', 'Collapsed');
}
else {
$('.ExpandCollapseGrandStyle').attr('src', 'images/minus.gif');
$('.ExpandCollapseGrandStyle').attr('alt', 'Expanded');
}
})
function isDisplayed(object) {
// if the object is visible return true
if ($(object).css('display') == 'block') {
return true;
}
// if the object is not visible return false
return false;
};
});
</script>
</body>
</html>
This is c# 这是C#
// To keep track of the previous row Group Identifier
string strPreviousRowID = string.Empty;
// To keep track the Index of Group Total
int intSubTotalIndex = 1;
string strGroupHeaderText = string.Empty;
// To temporarily store Sub Total
double dblSubTotalUnitPrice = 0;
double dblSubTotalQuantity = 0;
double dblSubTotalDiscount = 0;
double dblSubTotalAmount = 0;
// To temporarily store Grand Total
double dblGrandTotalUnitPrice = 0;
double dblGrandTotalQuantity = 0;
double dblGrandTotalDiscount = 0;
double dblGrandTotalAmount = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// Event fires for every row creation
/// Used for creating SubTotal row when next group starts by adding Group Total at previous row manually
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void grdViewOrders_RowCreated(object sender, GridViewRowEventArgs e)
{
bool IsSubTotalRowNeedToAdd = false;
bool IsGrandTotalRowNeedtoAdd = false;
if ((strPreviousRowID != string.Empty) && (DataBinder.Eval(e.Row.DataItem, "CustomerID") != null))
if (strPreviousRowID != DataBinder.Eval(e.Row.DataItem, "CustomerID").ToString())
IsSubTotalRowNeedToAdd = true;
if ((strPreviousRowID != string.Empty) && (DataBinder.Eval(e.Row.DataItem, "CustomerID") == null))
{
IsSubTotalRowNeedToAdd = true;
IsGrandTotalRowNeedtoAdd = true;
intSubTotalIndex = 0;
}
#region Getting the first Group Header Text
if ((strPreviousRowID == string.Empty) && (DataBinder.Eval(e.Row.DataItem, "CustomerID") != null))
strGroupHeaderText = DataBinder.Eval(e.Row.DataItem, "CompanyName").ToString();
#endregion
if (IsSubTotalRowNeedToAdd)
{
#region Adding Sub Total Row
GridView grdViewOrders = (GridView)sender;
// Creating a Row
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
//Adding Group Expand Collapse Cell
TableCell cell = new TableCell();
row.Cells.Add(cell);
//Adding Expand Collapse Cell
cell = new TableCell();
System.Web.UI.HtmlControls.HtmlImage img = new System.Web.UI.HtmlControls.HtmlImage();
img.Src = "images/minus.gif";
img.Attributes.Add("alt", strPreviousRowID);
img.Attributes.Add("class", "ExpandCollapseStyle");
cell.Controls.Add(img);
cell.HorizontalAlign = HorizontalAlign.Left;
cell.CssClass = "SubTotalRowStyle";
row.Cells.Add(cell);
//Adding Header Cell
cell = new TableCell();
cell.Text = strGroupHeaderText;
cell.HorizontalAlign = HorizontalAlign.Left;
cell.ColumnSpan = 2;
cell.CssClass = "SubTotalRowStyle";
row.Cells.Add(cell);
//Adding Unit Price Column
cell = new TableCell();
cell.Text = string.Format("{0:0.00}", dblSubTotalUnitPrice);
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "SubTotalRowStyle";
row.Cells.Add(cell);
//Adding Quantity Column
cell = new TableCell();
cell.Text = string.Format("{0:0.00}", dblSubTotalQuantity);
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "SubTotalRowStyle";
row.Cells.Add(cell);
//Adding Discount Column
cell = new TableCell();
cell.Text = string.Format("{0:0.00}", dblSubTotalDiscount);
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "SubTotalRowStyle";
row.Cells.Add(cell);
//Adding Amount Column
cell = new TableCell();
cell.Text = string.Format("{0:0.00}", dblSubTotalAmount);
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "SubTotalRowStyle";
row.Cells.Add(cell);
//Adding the Row at the RowIndex position in the Grid
grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);
intSubTotalIndex++;
#endregion
#region Getting Next Group Header Details
if (DataBinder.Eval(e.Row.DataItem, "CustomerID") != null)
strGroupHeaderText = DataBinder.Eval(e.Row.DataItem, "CompanyName").ToString();
#endregion
#region Reseting the Sub Total Variables
dblSubTotalUnitPrice = 0;
dblSubTotalQuantity = 0;
dblSubTotalDiscount = 0;
dblSubTotalAmount = 0;
#endregion
}
if (IsGrandTotalRowNeedtoAdd)
{
#region Grand Total Row
GridView grdViewOrders = (GridView)sender;
// Creating a Row
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
//Adding Group Expand Collapse Cell
TableCell cell = new TableCell();
System.Web.UI.HtmlControls.HtmlImage img = new System.Web.UI.HtmlControls.HtmlImage();
img.Src = "images/minus.gif";
img.Attributes.Add("class", "ExpandCollapseGrandStyle");
img.Attributes.Add("alt", "Expanded");
cell.Controls.Add(img);
cell.HorizontalAlign = HorizontalAlign.Left;
cell.CssClass = "GrandTotalRowStyle";
row.Cells.Add(cell);
//Adding Expand Collapse Cell
cell = new TableCell();
cell.CssClass = "GrandTotalRowStyle";
row.Cells.Add(cell);
//Adding Header Cell
cell = new TableCell();
cell.Text = "Grand Total";
cell.HorizontalAlign = HorizontalAlign.Left;
cell.ColumnSpan = 2;
cell.CssClass = "GrandTotalRowStyle";
row.Cells.Add(cell);
//Adding Unit Price Column
cell = new TableCell();
cell.Text = string.Format("{0:0.00}", dblGrandTotalUnitPrice);
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "GrandTotalRowStyle";
row.Cells.Add(cell);
//Adding Quantity Column
cell = new TableCell();
cell.Text = string.Format("{0:0.00}", dblGrandTotalQuantity);
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "GrandTotalRowStyle";
row.Cells.Add(cell);
//Adding Discount Column
cell = new TableCell();
cell.Text = string.Format("{0:0.00}", dblGrandTotalDiscount);
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "GrandTotalRowStyle";
row.Cells.Add(cell);
//Adding Amount Column
cell = new TableCell();
cell.Text = string.Format("{0:0.00}", dblGrandTotalAmount);
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "GrandTotalRowStyle";
row.Cells.Add(cell);
//Adding the Row at the RowIndex position in the Grid
grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex, row);
#endregion
}
}
/// <summary>
/// Event fires when data binds to each row
/// Used for calculating Group Total
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void grdViewOrders_RowDataBound(object sender, GridViewRowEventArgs e)
{
// This is for cumulating the values
if (e.Row.RowType == DataControlRowType.DataRow)
{
strPreviousRowID = DataBinder.Eval(e.Row.DataItem, "CustomerID").ToString();
double dblUnitPrice = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "UnitPrice").ToString());
double dblQuantity = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Quantity").ToString());
double dblDiscount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Discount").ToString());
double dblAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Amount").ToString());
// Cumulating Sub Total
dblSubTotalUnitPrice += dblUnitPrice;
dblSubTotalQuantity += dblQuantity;
dblSubTotalDiscount += dblDiscount;
dblSubTotalAmount += dblAmount;
// Cumulating Grand Total
dblGrandTotalUnitPrice += dblUnitPrice;
dblGrandTotalQuantity += dblQuantity;
dblGrandTotalDiscount += dblDiscount;
dblGrandTotalAmount += dblAmount;
e.Row.Style.Add("display", "block");
e.Row.CssClass = "ExpandCollapse" + strPreviousRowID;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.