Gridview Code

DML Operations with Gridview without using Template Field

DML Operations with Gridview without using Template Field

This is the HTMLcode for the GridView.
<asp:GridView ID="studentsGrid" runat="server" AutoGenerateColumns="false" DataKeyNames="Id" 
        Width="100%" onrowcancelingedit="studentsGrid_RowCancelingEdit"
        onrowdeleting="studentsGrid_RowDeleting" onrowediting="studentsGrid_RowEditing" 
        onrowupdating="studentsGrid_RowUpdating">

<Columns>

<asp:TemplateField HeaderText="S.NO">
<ItemTemplate>
<%# Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField HeaderText="Name" DataField="Name"/>
<asp:BoundField HeaderText="Email" DataField="Email" />
<asp:BoundField HeaderText="Class" DataField="Class" />
<asp:BoundField HeaderText="Section" DataField="Section" />
<asp:BoundField HeaderText="Status" DataField="Status" />
<asp:CommandField ShowEditButton="true" HeaderText="Edit" />
<asp:CommandField ShowDeleteButton="true" HeaderText="Delete"/>

</Columns>

<HeaderStyle HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />

</asp:GridView>




This is the Code Behind File for the GridView.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection("Data Source=ATLANTA5;Initial Catalog=MYDB;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();

    protected void BindGV()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from tb_student", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        studentsGrid.DataSource = dt;
        studentsGrid.DataBind();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGV();
        }
    }


    protected void studentsGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        studentsGrid.EditIndex = e.NewEditIndex;
        BindGV();
    }


    protected void studentsGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update tb_student set Name=@name, email=@email,class=@class, section=@section,status=@status where Id=@Id";

        GridViewRow R = studentsGrid.Rows[e.RowIndex];


        TextBox t1 = (TextBox)R.Cells[1].Controls[0];
        cmd.Parameters.AddWithValue("@Name", t1.Text);

        t1 = (TextBox)R.Cells[2].Controls[0];
        cmd.Parameters.AddWithValue("@email", t1.Text);

        t1 = (TextBox)R.Cells[4].Controls[0];
        cmd.Parameters.AddWithValue("@class", t1.Text);

        t1 = (TextBox)R.Cells[5].Controls[0];
        cmd.Parameters.AddWithValue("@section", t1.Text);

        t1 = (TextBox)R.Cells[6].Controls[0];
        cmd.Parameters.AddWithValue("@status", t1.Text);

        int id = Convert.ToInt32(studentsGrid.DataKeys[e.RowIndex].Value);
        cmd.Parameters.AddWithValue("@Id", id);

        con.Open();
        int res = cmd.ExecuteNonQuery();
        con.Close();

        studentsGrid.EditIndex = -1;
        BindGV();

    }


    protected void studentsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(studentsGrid.DataKeys[e.RowIndex].Value);
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "delete from tb_student where Id=@Id";
        cmd.Parameters.AddWithValue("@Id", id);

        con.Open();
        int res = cmd.ExecuteNonQuery();
        con.Close();

        BindGV();
    }


    protected void studentsGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        studentsGrid.EditIndex = -1;
        BindGV();
    }

}

Note: 

If you want to have any web controls in the footer row of any specific column, then use Templatefield for that column. Other wise you can use BoundField. If you want to have web controls in the footer row for all the columns, then it is better to use templatefield for all the columns. Please view the next example.

DML Operations with Gridview using Template Field

DML Operations with Gridview using Template Field

This is the HTMLcode for the GridView.
<asp:GridView ID="studentsGrid" runat="server" AutoGenerateColumns="false" DataKeyNames="Id"
        Width="100%" ShowFooter="true" onrowediting="studentsGrid_RowEditing"
        onrowcommand="studentsGrid_RowCommand"
        onrowcancelingedit="studentsGrid_RowCancelingEdit"
        onrowdeleting="studentsGrid_RowDeleting"
        onrowupdating="studentsGrid_RowUpdating">

<Columns>

<asp:TemplateField HeaderText="S.NO">
<ItemTemplate>
<%# Container.DataItemIndex+1 %>
</ItemTemplate>

<FooterTemplate>
<asp:Button runat="server" Text="Add" ID="btninsert" CommandName="footer" />
</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Name">

<ItemTemplate>
<%#Eval("Name") %>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" ID="lbleditName" Text='<%#Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>

<FooterTemplate>
<asp:TextBox runat="server" ID="fname"></asp:TextBox>
</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Email">

<ItemTemplate>
<%#Eval("Email")%>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" ID="lbleditEmail" Text='<%#Eval("Email") %>'></asp:TextBox>
</EditItemTemplate>

<FooterTemplate>
<asp:TextBox runat="server" ID="fEmail"></asp:TextBox>
</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Class">

<ItemTemplate>
<%#Eval("Class")%>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" ID="lbleditClass" Text='<%#Eval("Class") %>'></asp:TextBox>
</EditItemTemplate>

<FooterTemplate>
<asp:TextBox runat="server" ID="fClass"></asp:TextBox>
</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Section">

<ItemTemplate>
<%#Eval("Section")%>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" ID="lbleditSection" Text='<%#Eval("Section") %>'></asp:TextBox>
</EditItemTemplate>

<FooterTemplate>
<asp:TextBox runat="server" ID="fSection"></asp:TextBox>
</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Status">

<ItemTemplate>
<%#Eval("Status")%>
</ItemTemplate>

<EditItemTemplate>
<asp:RadioButtonList runat="server" ID="editrbl" >
<asp:ListItem Text="Active" Value="1"  Selected="True"></asp:ListItem>
<asp:ListItem Text="InActive" Value="0"></asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>

<FooterTemplate>
<asp:RadioButtonList runat="server" ID="frbl" RepeatDirection="Horizontal">
    <asp:ListItem Selected="True" Value="1">True</asp:ListItem>
    <asp:ListItem Value="0">False</asp:ListItem>
    </asp:RadioButtonList>
</FooterTemplate>

</asp:TemplateField>


<asp:TemplateField HeaderText="Edit" >

<ItemTemplate>
<asp:ImageButton ID="Image1" ImageUrl="~/images/Edit.png" width="40px" height="40px" runat="server" ToolTip="Edit" CommandName="Edit"  />
</ItemTemplate>

<EditItemTemplate>
<asp:ImageButton runat="server" ImageUrl="~/images/system-software-update.png" width="40px" height="40px" ToolTip="Update" CommandName="Update" />
<asp:ImageButton runat="server" ImageUrl="~/images/file_document_doc_del_paper_cancel.png"  width="40px" height="40px" ToolTip="Cancel" CommandName="Cancel" />
</EditItemTemplate>

</asp:TemplateField>



<asp:TemplateField HeaderText="Delete">

<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/Delete.png"  width="40px" height="40px" ToolTip="Cancel" CommandName="Delete" OnClientClick="return confirm('Are you Sure want to Delete')" />
</ItemTemplate>
</asp:TemplateField>

</Columns>

<HeaderStyle HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
<FooterStyle HorizontalAlign="Center" />

</asp:GridView>




This is the Code Behind File for the GridView.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=ATLANTA5;Initial Catalog=MYDB;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    protected void BindGV()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from tb_student", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        studentsGrid.DataSource = dt;
        studentsGrid.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGV();
        }
    }


    protected void studentsGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        studentsGrid.EditIndex = e.NewEditIndex;
        BindGV();
    }


    protected void studentsGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        studentsGrid.EditIndex = -1;
        BindGV();
    }


    protected void studentsGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update tb_student set Name=@name, email=@email, class=@class, section=@section,status=@status where Id=@Id";

        GridViewRow R = studentsGrid.Rows[e.RowIndex];


        //TextBox t1 = (TextBox)R.Cells[1].Controls[0]; if I use this, the following error message is appearing:
        //Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.

        TextBox t1 = R.FindControl("lbleditName") as TextBox;
        cmd.Parameters.AddWithValue("@Name", t1.Text);

        ////t1 = (TextBox)R.Cells[2].Controls[0];
        t1 = R.FindControl("lbleditEmail") as TextBox;
        cmd.Parameters.AddWithValue("@email", t1.Text);

        //t1 = (TextBox)R.Cells[3].Controls[0];
        t1 = R.FindControl("lbleditClass") as TextBox;
        cmd.Parameters.AddWithValue("@class", t1.Text);

        ////t1 = (TextBox)R.Cells[4].Controls[0];
        t1 = R.FindControl("lbleditSection") as TextBox;
        cmd.Parameters.AddWithValue("@section", t1.Text);

        //t1 = (TextBox)R.Cells[5].Controls[0];
        RadioButtonList rbl = R.FindControl("editrbl") as RadioButtonList;
        cmd.Parameters.AddWithValue("@status", rbl.SelectedValue);


        int id = Convert.ToInt32(studentsGrid.DataKeys[e.RowIndex].Value);
        cmd.Parameters.AddWithValue("@Id", id);

        con.Open();
        int res = cmd.ExecuteNonQuery();
        con.Close();

        studentsGrid.EditIndex = -1;
        BindGV();

    }


    protected void studentsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(studentsGrid.DataKeys[e.RowIndex].Value);
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "delete from tb_student where Id=@Id";
        cmd.Parameters.AddWithValue("@Id", id);

        con.Open();
        int res = cmd.ExecuteNonQuery();
        con.Close();

        BindGV();
    }


    protected void studentsGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "footer")
        {
            GridViewRow R = studentsGrid.FooterRow;

            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO [tb_student]" +
                               "([Name],[email],[Class]" +
                               ",[Section],[Status]) VALUES" +
                                "(@Name,@email,@Class,@Section,@Status)";


            TextBox t1 = (TextBox)R.FindControl("fname");
            cmd.Parameters.AddWithValue("@Name", t1.Text);

            t1 = (TextBox)R.FindControl("fEmail");
            cmd.Parameters.AddWithValue("@email", t1.Text);

            t1 = (TextBox)R.FindControl("fclass");
            cmd.Parameters.AddWithValue("@class", t1.Text);

            t1 = (TextBox)R.FindControl("fsection");
            cmd.Parameters.AddWithValue("@section", t1.Text);

            RadioButtonList rbl = (RadioButtonList)R.FindControl("frbl");
            cmd.Parameters.AddWithValue("@status", rbl.SelectedValue);

            con.Open();
            int res = cmd.ExecuteNonQuery();
            con.Close();
            BindGV();

        }


    }


}

Note: 

Here we have used the custom commandfields using images. For example, in the previous example, we have used <asp:commandfield showeditbutton="true"/>. This indicates a button with Text Edit with Command Name set to Edit. You can also use your own Image button and set the Command name to "Edit", so that it supports Gridview_RowEditing() Event. The same with Update and Delete fields. You can use any image and set the Command name to Update and Delete so that they will support Gridview_RowUpdating and Gridview_RowDeleting Events respectively. Also, if you are using the Templatefield, you should use the EditItemTemplate to get the textbox or any other controls in the edit mode. You should declare the textbox or any other controls in the edititemtemplates. If you're using Boundfield, the textboxes will be generated in the edit mode by default.

DML Operations with Gridview using RowCommand Event

DML Operations with Gridview using RowCommand Event

We are using the previous Example to illustrate the code. This is the HTMLcode for the GridView.


<asp:GridView ID="studentsGrid" runat="server" AutoGenerateColumns="false" DataKeyNames="Id"
        Width="100%" ShowFooter="true" onrowediting="studentsGrid_RowEditing"
        onrowcommand="studentsGrid_RowCommand"
        onrowcancelingedit="studentsGrid_RowCancelingEdit"
        onrowdeleting="studentsGrid_RowDeleting"
        onrowupdating="studentsGrid_RowUpdating">

<Columns>

<asp:TemplateField HeaderText="S.NO">
<ItemTemplate>
<%# Container.DataItemIndex+1 %>
</ItemTemplate>

<FooterTemplate>
<asp:Button runat="server" Text="Add" ID="btninsert" CommandName="footer" />
</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Name">

<ItemTemplate>
<%#Eval("Name") %>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" ID="lbleditName" Text='<%#Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>

<FooterTemplate>
<asp:TextBox runat="server" ID="fname"></asp:TextBox>
</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Email">

<ItemTemplate>
<%#Eval("Email")%>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" ID="lbleditEmail" Text='<%#Eval("Email") %>'></asp:TextBox>
</EditItemTemplate>

<FooterTemplate>
<asp:TextBox runat="server" ID="fEmail"></asp:TextBox>
</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Class">

<ItemTemplate>
<%#Eval("Class")%>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" ID="lbleditClass" Text='<%#Eval("Class") %>'></asp:TextBox>
</EditItemTemplate>

<FooterTemplate>
<asp:TextBox runat="server" ID="fClass"></asp:TextBox>
</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Section">

<ItemTemplate>
<%#Eval("Section")%>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" ID="lbleditSection" Text='<%#Eval("Section") %>'></asp:TextBox>
</EditItemTemplate>

<FooterTemplate>
<asp:TextBox runat="server" ID="fSection"></asp:TextBox>
</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Status">

<ItemTemplate>
<%#Eval("Status")%>
</ItemTemplate>

<EditItemTemplate>
<asp:RadioButtonList runat="server" ID="editrbl" >
<asp:ListItem Text="Active" Value="1"  Selected="True"></asp:ListItem>
<asp:ListItem Text="InActive" Value="0"></asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>

<FooterTemplate>
<asp:RadioButtonList runat="server" ID="frbl" RepeatDirection="Horizontal">
    <asp:ListItem Selected="True" Value="1">True</asp:ListItem>
    <asp:ListItem Value="0">False</asp:ListItem>
    </asp:RadioButtonList>
</FooterTemplate>

</asp:TemplateField>


<asp:TemplateField HeaderText="Edit" >

<ItemTemplate>
<asp:ImageButton ID="Image1" ImageUrl="~/images/Edit.png" width="40px" height="40px" runat="server" ToolTip="Edit" CommandName="Edit"  />
</ItemTemplate>

<EditItemTemplate>
<asp:ImageButton runat="server" ImageUrl="~/images/system-software-update.png" width="40px" height="40px" ToolTip="Update" CommandName="Update" />
<asp:ImageButton runat="server" ImageUrl="~/images/file_document_doc_del_paper_cancel.png"  width="40px" height="40px" ToolTip="Cancel" CommandName="Cancel" />
</EditItemTemplate>

</asp:TemplateField>



<asp:TemplateField HeaderText="Delete">

<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/Delete.png"  width="40px" height="40px" ToolTip="Cancel" CommandName="Delete" OnClientClick="return confirm('Are you Sure want to Delete')" />
</ItemTemplate>
</asp:TemplateField>

</Columns>

<HeaderStyle HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
<FooterStyle HorizontalAlign="Center" />

</asp:GridView>



This is the Code Behind File for the GridView.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=ATLANTA5;Initial Catalog=MYDB;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    protected void BindGV()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from tb_student", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        studentsGrid.DataSource = dt;
        studentsGrid.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGV();
        }
    }


    protected void studentsGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        studentsGrid.EditIndex = e.NewEditIndex;
        BindGV();
    }


    protected void studentsGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        studentsGrid.EditIndex = -1;
        BindGV();
    }


    protected void studentsGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
      
        

    }


    protected void studentsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
     
    }


    protected void studentsGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;

        if (e.CommandName == "footer")
        {
            GridViewRow R = studentsGrid.FooterRow;

            cmd.CommandText = "INSERT INTO [tb_student]" +
                               "([Name],[email],[Class]" +
                               ",[Section],[Status]) VALUES" +
                                "(@Name,@email,@Class,@Section,@Status)";


            TextBox t1 = (TextBox)R.FindControl("fname");
            cmd.Parameters.AddWithValue("@Name", t1.Text);

            t1 = (TextBox)R.FindControl("fEmail");
            cmd.Parameters.AddWithValue("@email", t1.Text);

            t1 = (TextBox)R.FindControl("fclass");
            cmd.Parameters.AddWithValue("@class", t1.Text);

            t1 = (TextBox)R.FindControl("fsection");
            cmd.Parameters.AddWithValue("@section", t1.Text);

            RadioButtonList rbl = (RadioButtonList)R.FindControl("frbl");
            cmd.Parameters.AddWithValue("@status", rbl.SelectedValue);

            con.Open();
            int res = cmd.ExecuteNonQuery();
            con.Close();
            BindGV();

        }


        else if (e.CommandName == "Update")
        {

            cmd.CommandText = "update tb_student set Name=@name, email=@email, class=@class, section=@section,status=@status where Id=@Id";

            GridViewRow gvrow = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;

            int id = Convert.ToInt32(studentsGrid.DataKeys[gvrow.RowIndex].Value);
            cmd.Parameters.AddWithValue("@Id", id);

            TextBox name = (TextBox)gvrow.FindControl("lbleditName");
            cmd.Parameters.AddWithValue("@Name", name.Text);

            TextBox email = (TextBox)gvrow.FindControl("lbleditEmail");
            cmd.Parameters.AddWithValue("@email", email.Text);

            TextBox class1 = (TextBox)gvrow.FindControl("lbleditClass");
            cmd.Parameters.AddWithValue("@class", class1.Text);

            TextBox Section = (TextBox)gvrow.FindControl("lbleditSection");
            cmd.Parameters.AddWithValue("@section", Section.Text);

            RadioButtonList rbl = (RadioButtonList)gvrow.FindControl("editrbl");
            cmd.Parameters.AddWithValue("@status", rbl.SelectedValue);

            con.Open();
            int res = cmd.ExecuteNonQuery();
            con.Close();

            if (res > 0)
            {
                studentsGrid.EditIndex = -1;
                BindGV();
            }

        }


        else if (e.CommandName == "Delete")
        {
            GridViewRow gvrow = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;

            int d_id = Convert.ToInt32(studentsGrid.DataKeys[gvrow.RowIndex].Value);//You can also use the CommandArgumentpropertyforthe Delete Button and use the e.CommandArgument option to get the ID


            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "delete from tb_student where Id=@Id";
            cmd.Parameters.AddWithValue("@Id", d_id);


            con.Open();
            int res = cmd.ExecuteNonQuery();
            con.Close();

            BindGV();

        }


    }



}

Sum of Salary using Gridview Footer Row

Sum of Salary using Gridview's Footer Row



This is the HTMLcode for the GridView.
 <asp:GridView runat="server" ID="empGV" AutoGenerateColumns="false" Width="100%"
        ShowFooter="true" OnRowDataBound="empGV_RowDataBound">
        <Columns>

            <asp:BoundField DataField="EMPID" HeaderText="EMP ID" />

            <asp:BoundField DataField="Name" HeaderText="Name" />

            <asp:TemplateField HeaderText="Location">
                <ItemTemplate>
                    <%#Eval("Location")%>
                </ItemTemplate>

                <FooterTemplate>
                    <asp:Label runat="server" ID="lbl" Text="Total:"></asp:Label>
                </FooterTemplate>

            </asp:TemplateField>


            <asp:TemplateField HeaderText="Salary">

                <ItemTemplate>
                    <%#Eval("Salary")%>
                </ItemTemplate>

                <FooterTemplate>
                    <asp:Label runat="server" ID="lblTotal"></asp:Label>
                </FooterTemplate>

            </asp:TemplateField>


        </Columns>

        <HeaderStyle HorizontalAlign="Center" />
        <RowStyle HorizontalAlign="Center" />
        <FooterStyle Font-Bold="true" HorizontalAlign="Right" />

    </asp:GridView>


This is the Code Behind File for the GridView.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default4 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=ATLANTA5;Initial Catalog=MYDB;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();

    protected void BindGV()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from tb_Emp", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        empGV.DataSource = dt;
        empGV.DataBind();

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGV();
        }

    }


    int total = 0;
    protected void empGV_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Salary"));
        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lbl = e.Row.FindControl("lblTotal") as Label;
            lbl.Text = total.ToString("c");
        }
    }


}