Jitesh Byahut 2/25/2014 45450

How To Insert New Row In GridView In Asp.Net

Here we are binding a GridView in Asp.Net and C#.

Points Of Remember:

1. Place a TextBox in all column of FooterTemplate except first column.

2. Fire GridView's SeletedIndexChanging event.

3. Place a LinkButton with CommandName Select in first column of FooterTemplate.

4. Add namespace System.Data and System.Data.SqlClient in your C# page.

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>How to Insert new row in GridView</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4"  

         ForeColor="#333333" ShowFooter="True"

         OnSelectedIndexChanging="GridView1_SelectedIndexChanging" >

          <Columns>

           <asp:TemplateField>

              <FooterTemplate>

               <asp:LinkButton ID="LkB1" runat="server" CommandName="Select">Insert</asp:LinkButton>

              </FooterTemplate>

           </asp:TemplateField>

           <asp:TemplateField HeaderText="Name">

              <ItemTemplate>

                 <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>

              </ItemTemplate>

              <FooterTemplate>

                 <asp:TextBox ID="txt_Name" runat="server"></asp:TextBox>

               </FooterTemplate>

           </asp:TemplateField>

           <asp:TemplateField HeaderText="Branch">

               <ItemTemplate>

                  <asp:Label ID="Label2" runat="server" Text='<%# Eval("Branch")%>'></asp:Label>

               </ItemTemplate>

               <FooterTemplate>

                  <asp:TextBox ID="txt_Branch" runat="server"></asp:TextBox>

               </FooterTemplate>

            </asp:TemplateField>

            <asp:TemplateField HeaderText="City">

               <ItemTemplate>

                  <asp:Label ID="Label3" runat="server" Text='<%# Eval("City") %>'></asp:Label>

               </ItemTemplate>

               <FooterTemplate>

                  <asp:TextBox ID="txt_City" runat="server"></asp:TextBox>

               </FooterTemplate>

            </asp:TemplateField>

          </Columns> 

        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />       

        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

        </asp:GridView>

    </div>

    </form>

</body>

</html>

C# Codes :

using System.Data;

using System.Data.SqlClient;

 

public partial class GridView_Insertrow : System.Web.UI.Page

{

    // sql connection

    SqlConnection con = new SqlConnection(@"Data Source=JITESH-PC\SQL;Initial

                                                                                  Catalog=db_Student;Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            BindGridView();

        }

    }

 

    //method for binding GridView

    protected void BindGridView()

    {

        DataTable dt = new DataTable();

        SqlDataAdapter da = new SqlDataAdapter("Select ID, Name,Branch,City from tbl_student", con);

        con.Open();

        da.Fill(dt);

        con.Close();

 

        if (dt.Rows.Count > 0)

        {

            GridView1.DataSource = dt;

            GridView1.DataBind();

        }

    }

 

    // insert new record in database

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)

    {

        // find values for update

        TextBox txtname = (TextBox)GridView1.FooterRow.FindControl("txt_Name");

        TextBox txtbranch = (TextBox)GridView1.FooterRow.FindControl("txt_Branch");

        TextBox txtcity = (TextBox)GridView1.FooterRow.FindControl("txt_City");

 

        // insert values into database

        SqlCommand cmd = new SqlCommand("insert into tbl_student(Name, Branch, City)

                          values('" + txtname.Text + "','" + txtbranch.Text + "','" + txtcity.Text + "')", con);

        con.Open();

        cmd.ExecuteNonQuery();

        con.Close();

 

        BindGridView();

    }

}

View output :

demo


Please give your feedback for improving this page