Thursday, 31 May 2012

Sum of Gridview column 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
    .FooterStyle
{
    background-color: #a33;
    color: White;
    text-align: right;
}
    </style>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>
    <asp:GridView ID="GV1" runat="server"
        AutoGenerateColumns = "false" Font-Names = "Arial"
        Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
        HeaderStyle-BackColor = "green" AllowPaging ="true"  
        PageSize = "10" Width="345px"
            ShowFooter="true" FooterStyle-CssClass="FooterStyle" >
<Columns>
<asp:TemplateField HeaderText="Emp Id">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("emp_id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp Name">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%#Eval("emp_name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone">
<ItemTemplate>
<asp:Label ID="lblphone" runat="server" Text='<%#Eval("phone") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lbltotal" runat="server" Text="Total Salary"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<asp:Label ID="lblsalary" runat="server" Text='<%#Eval("salary") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblsum" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="#C2D69B"  />

<HeaderStyle BackColor="Green"></HeaderStyle>
</asp:GridView>
   
    </div>
    </div>
    </form>
</body>
</html>
.cs code
public partial class GridView2_sum_ofcolumn : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            grid_bind();

        }
    }

    public void grid_bind()
    {
        try
        {
            con.Open();
            //string str = "skgridview1_ins";
            da = new SqlDataAdapter("select *from skgridview1",con);
            da.Fill(ds);
            GV1.DataSource = ds;
            GV1.DataBind();
            con.Close();   
((Label)GV1.FooterRow.Cells[1].FindControl("lblsum")).Text = ds.Tables[0].Compute("sum(salary)", "").ToString();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

OR

 private decimal _TotalsalTotal = 0;
    protected void GV1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                decimal Totalsal = (decimal)DataBinder.Eval(e.Row.DataItem, "Salary");
                _TotalsalTotal += Totalsal;
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lblSummary = (Label)e.Row.FindControl("lblsum");
                lblSummary.Text = String.Format("{0:c}", _TotalsalTotal);
            }
        }

No comments:

Post a Comment