Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Friday, September 14, 2007

Nested GridView with subtotals, totals, and counts

This complicated example shows a nested GridView with subtotals, totals, and counts. The actual process of modifying the GridView ItemTemplates is not explained here. Note: The SQL should be replaced with a stored procedure for production.
NestedGridView example


<script runat="server">
Decimal masterTotal = 0;
Decimal masterCount = 0;
Decimal priceTotal = 0;
int priceCount = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlDataSource s = (SqlDataSource)e.Row.FindControl("SqlDataSource2");
String str = e.Row.Cells[0].Text;
s.SelectParameters[0].DefaultValue = e.Row.Cells[0].Text;

}
}

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
// add the UnitPrice and QuantityTotal to the running total variables
priceTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "contribution_amount"));
priceCount++;
masterCount++;
}
else if ((e.Row.RowType == DataControlRowType.Footer) )
{
masterTotal += priceTotal;
if (priceCount > 1)
{
e.Row.Cells[0].Text = "Subtotal:";
// for the Footer, display the running totals
e.Row.Cells[2].Text = priceCount.ToString();
e.Row.Cells[3].Text = priceTotal.ToString("c");
e.Row.Cells[3].HorizontalAlign = e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
e.Row.Font.Bold = true;
{
GridView1.FooterRow.Cells[0].Text = "Total:";
// for the Footer, display the running totals
GridView1.FooterRow.Cells[1].Text = masterCount.ToString();
GridView1.FooterRow.Cells[1].Text += "&nbsp&nbsp&nbsp&nbsp" + masterTotal.ToString("c");
GridView1.FooterRow.Cells[1].HorizontalAlign = GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right;
GridView1.FooterRow.Font.Bold = true;
}
}
else if (priceCount == 1)
{// blank out borders and background color for 1 row footers

e.Row.Visible = false;
}
else
{
e.Row.Visible = false;
}
priceTotal = 0;
priceCount = 0;
}
}


</script>

Here is the html part of the aspx:


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Campaign Finance Contributions By Date</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td align="center" nowrap="nowrap" style="width: 253px; height: 92px">
<asp:Image ID="TelWebImage" runat="server" ImageAlign="Middle" ImageUrl="~/tel-web.gif"
Style="z-index: 101; left: 7px; position: relative; top: 9px" /></td>
</tr>
<tr>
<td align="center" nowrap="nowrap" style="width: 253px; height: 42px">
<asp:Label ID="TitleLabel" runat="server" Font-Size="Larger" Style="z-index: 100;
left: 4px; position: relative; top: 0px"
Text="Campaign Finance Contributions"
Width="206px"></asp:Label></td>
</tr>
<tr>
<td align="center" nowrap="nowrap" style="width: 253px">
<asp:Label ID="ByDateLabel" runat="server" Style="z-index: 102; left: 16px; position: relative;
top: 0px"
Text="BY CONTRIBUTION DATE"></asp:Label></td>
</tr>
<tr>
<td align="center" nowrap="nowrap" style="width: 253px; height: 19px">
<asp:Label ID="DateTimeLab" runat="server" Style="left: 0px; position: relative;
top: 2px"
Text="Date and Time"></asp:Label></td>
</tr>
</table>
</div>
&nbsp;&nbsp;
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT contribution_date FROM ContributionCandidateview WHERE (contribution_number LIKE @contribution_number)">
<SelectParameters>
<asp:Parameter DefaultValue="%" Name="contribution_number" />
</SelectParameters>
</asp:SqlDataSource>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"></asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True">
<Columns>
<asp:BoundField DataField="contribution_date" HeaderText="contribution_date" SortExpression="contribution_date" />
<asp:TemplateField HeaderText="Contributions">
<ItemTemplate>
&nbsp;
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [candidate_name], [contribution_date], [contribution_name1], [contribution_amount] FROM [ContributionCandidateview] WHERE ([contribution_date] = @contribution_date)">
<SelectParameters>
<asp:Parameter Name="contribution_date" Type="DateTime" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView2" runat="server" DataSourceID="SqlDataSource2" OnRowDataBound="GridView2_RowDataBound" ShowFooter="True" Width="500px">
<AlternatingRowStyle BorderColor="White" BackColor="#E0E0E0" />
<FooterStyle BackColor="#DFFFDF" />
<HeaderStyle BackColor="#DFDFFF" />
</asp:GridView>
</ItemTemplate>
<ItemStyle VerticalAlign="Bottom" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#DFDFFF" />
</asp:GridView>
</form>
</body>
</html>

5 comments:

Unknown said...

Hi, do You have an exmaple with GridView ItemTemplates?

Anonymous said...

Understandably your article helped me terribly much in my college assignment. Hats incorrect to you post, choice look ahead for the duration of more cognate articles without delay as its one of my pet issue to read.

Anonymous said...

Use   with the semicolon instead of &nbsp

Anonymous said...

Sorry for my bad english. Thank you so much for your good post. Your post helped me in my college assignment, If you can provide me more details please email me.

Anonymous said...

vtyipmoetxdksw, vimax, PijrglvSm, [url=http://www.buypenisenlargement.com/]vimax[/url], reguwYjEfc, http://www.buypenisenlargement.com/ vimax, JpktrgLPHw, oyurciur Penis Enlargement, PijllllvSm, [url=http://www.male-sexual-styles.com/]Penis Enlargement[/url], uffffwYjEfc, http://www.male-sexual-styles.com/ Penis Enlargement, fdldgfkjldgfkhg
bigger penis, PijrglvSm, [url=http://www.buypenisenlargement.com/]bigger penis[/url], uwmnbmYjEfc, http://www.buypenisenlargement.com/ bigger penis, yhXdfhWDwh, Penis Enlargement pills, PiykkjlvSm, [url=http://www.buypenisenlargement.com/]Penis Enlargement pills[/url], uwYlkljEfc, http://www.buypenisenlargement.com/ Penis Enlargement pills, YdQtiQaCa, Penis Enlargement, PijtuklvSm, [url=http://www.buypenisenlargement.com/]Penis Enlargement[/url], uwYj66666jEfc, http://www.buypenisenlargement.com/ Penis Enlargement, XerrrGGhYG, Penis Enlargement, PijlwwwvSm, [url=http://www.male-sexual.com/]Penis Enlargement[/url], uwtttYjEfc, http://www.male-sexual.com/ Penis Enlargement, baeEcVyyyA, Penis Enlargement, PijlvSm, [url=http://penisenlargementkey.com/]Penis Enlargement[/url], uwYjEfc, http://penisenlargementkey.com/ Penis Enlargement, zpMpUeG, Penis Enlargement pills, PijlvSm, [url=http://penisenlargementkey.com/]Penis Enlargement pills[/url], uwYjEfc, http://penisenlargementkey.com/ Penis Enlargement pills, sWgergeyHm,
vimax, gwwefhhrglvSm, [url=http://vimax3-4.com/]vimax[/url], rfddfuwYjEfc, http://vimax3-4.com/ vimax, mRrfewfLitM.