Show Total In Grid View Footer
Some time you require to show total in footer of a Gridview which will generate sum of a column automatically.
With a brief explanation...
let take a table Table_1
CREATE TABLE [dbo].[Table_1]
(
[Sno] [int] IDENTITY(1,1) NOT NULL,
[Product] [varchar](50) NULL,
[Cost] [float] NULL
)
(
[Sno] [int] IDENTITY(1,1) NOT NULL,
[Product] [varchar](50) NULL,
[Cost] [float] NULL
)
which will give you this kind of GridView when Binding with it with some values..
Now you require to sum up cost in footer ,for that we'll take a Label On Footer of Cost ItemTemplate
as below ...
Below code is the only factor to calculate some ...
double totqty = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label totpcs = (Label)e.Row.FindControl("LabelC");
totqty = totqty + (Convert.ToInt32(totpcs.Text));
}
if (e.Row.RowType == DataControlRowType.Footer)
{
((Label)e.Row.FindControl("LabelFooter")).Text = totqty.ToString();
Label1.Text = totqty.ToString();//to make it visible outside grid
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label totpcs = (Label)e.Row.FindControl("LabelC");
totqty = totqty + (Convert.ToInt32(totpcs.Text));
}
if (e.Row.RowType == DataControlRowType.Footer)
{
((Label)e.Row.FindControl("LabelFooter")).Text = totqty.ToString();
Label1.Text = totqty.ToString();//to make it visible outside grid
}
here LabelC is lable on normaly showing in itetemplate
LabelFooter is lable on footer....
And Finally When You Execute The Programe Output Will Be Like That....
And here is finally what you're looking for
Good
ReplyDelete