welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Thursday, January 24, 2013

How to add or insert a new row on gridview in

Here I would like to share easy methods to insert new datarow in gridview there is no default insert row option like listview .however we can do by our self by using our tricks to accomplish insert new row features in
In a previous article I have wrote about insert ,edit and delete data in grdiview…For your reference I have mention the link as below…

In the code example, I have not been using database connetion; instead of I created dynamic data on datatable.
First I could start from the client side code…

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true"
            <asp:TemplateField HeaderText="Mark1">
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("m1") %>'></asp:TextBox>
            <asp:TemplateField HeaderText="Mark2">
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("m2") %>'></asp:TextBox>
            <asp:TemplateField HeaderText="Mark3">
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%#Eval("m3") %>'></asp:TextBox>
                    <asp:Button ID="Button1" runat="server" Text="ADD" OnClick="btnadd" />
        <FooterStyle HorizontalAlign="Right" />

In above code I used add button on the footer template, and don’t forget to add the code ShowFooter="true" on gridview tag, then only the add button will be visible on footer template.
Second we need to bind the data in gridview on Page load event..

protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

Here I wrote the separate function to bind the data on gridview using bind method

void bind()
        //Here you can call select query statement,get the data from database and
        //bind into gridview..
        //eg.sql="select * from test"
        //Get the data into datatabe
          dr = dt.NewRow();
       //Store the datatable value in session
        Session["dt"] = dt;
        //storing total rows count value
        Session["count"] = dt.Rows.Count;              
        GridView1.DataSource = dt;

Next  we write the code to on add button gridview .In a client I have added the btnadd function on button onclick event.

    public void btnadd(object sender,EventArgs e)
        int rwindex = (int)Session["count"] - 1;
        TextBox t1, t2, t3;
        //Here find the control and get the value from the gridview
        t1 = (TextBox)GridView1.Rows[rwindex].FindControl("textbox2");
        t2 = (TextBox)GridView1.Rows[rwindex].FindControl("textbox3");
        t3 = (TextBox)GridView1.Rows[rwindex].FindControl("textbox4");
        //Here you can insert the data into database by using SQL query
        //Once you insert into database and call the bind method
        //But here i used a datatable for sample....
        dt = (DataTable)Session["dt"];
        dr = dt.NewRow();
        dr["m1"] = t1.Text;
        dr["m2"] = t2.Text;
        dr["m3"] = t3.Text;
        dt.Rows.InsertAt(dr, rwindex);
        Session["dt"] = dt;
        Session["count"] = dt.Rows.Count;
        GridView1.DataSource = dt;

That’s it now you can insert the new row data on gridview.By simply clicking add button on gridview.
I hope you like the easiest way to insert new row on gridview..
Download the source code..
Post your comments and doubt about the article.