To compute the GridView Sum in asp.net let’s take a simple example of SQL Database table. For GridView sum in Asp.net you can use the following sample SQL table to test the source code given in this article.
CREATE TABLE [tbl_users]( [user_id] [int] IDENTITY(1,1) NOT NULL, [user_Fname] [varchar](50) NULL, [user_Lname] [varchar](50) NULL, [amount] [float] NULL )
Now you are ready to connect this SQL table with ASP.net web page to display the records in SQL table. But still you need a control to display the records in tabular format so that you may show the sum of column exactly below the Amount column of above created SQL table.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" CellPadding="4" ForeColor="#333333" GridLines="None">
<Columns>
<asp:BoundField DataField="user_Fname" HeaderText="firstName" />
<asp:BoundField DataField="user_Lname" HeaderText="lastName" FooterText="Total Balance" />
<asp:TemplateField HeaderText="Amount balance">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("amount") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label2" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
In the above HTML code of GridView control of ASP.Net 2.0 you can notice that we have used 2 bound columns to display the first and last name of user and we have used 1 template field to display the amount in Itemtemplate and sum of amount column in footertemplate. Set the FooterText of second bound field used to display the lastname so that it appears in front of column total.
After setting the GridView control, come to the main part of C# code. Do all the required code to connect the SQL Database to retrieve the records from the database table. If you don’t know the Database connectivity in ASP.Net 2.0 then learn from the article: ASP.Net Connect to SQL Database
SqlCommand mySQLcommand = new SqlCommand("select * from tbl_users", conn);
mySQLcommand.CommandType = CommandType.Text;
SqlDataAdapter mySQLadapter = new SqlDataAdapter (mySQLcommand);
DataSet myDataSet = new DataSet();
mySQLadapter.Fill(myDataSet);
GridView1.DataSource = myDataSet;
GridView1.DataBind();
((Label)GridView1. FooterRow. Cells[2]. FindControl("Label2")) .Text = myDataSet. Tables[0]. Compute("sum(amount)", ""). ToString();
In the above C# code Compute function is used that returns the passed expression for all the rows of a table in a dataset.
Compute function of DataTable in ASP.Net:
Compute function takes 2 parameters:
E.g.:
((Label)GridView1. FooterRow. Cells[2]. FindControl("Label2")) .Text = myDataSet. Tables[0]. Compute("sum(amount)", "user_id=1"). ToString();
Above code will compute the sum of amount for all the rows where user_id=1.
Currently rated 2.5 by 2 people
Tags: asp.net 2.0, gridview sum asp.net, sql database connection, asp.net gridview sum, sql connection string, c#, free source code
10/11/2008 3:34:52 AM