vijay

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 asp.net?

Here I would like to share easy methods to insert new datarow in asp.net.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 asp.net.
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"
        OnRowUpdating="GridView1_RowUpdating">
        <Columns>
            <asp:TemplateField HeaderText="Mark1">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("m1") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Mark2">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("m2") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Mark3">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%#Eval("m3") %>'></asp:TextBox>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Button ID="Button1" runat="server" Text="ADD" OnClick="btnadd" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle HorizontalAlign="Right" />
    </asp:GridView>
  

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)
        {
            bind();
        }
    }
  

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


void bind()
    {
        dt.Columns.Add("m1");
        dt.Columns.Add("m2");
        dt.Columns.Add("m3");
        //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();
          dt.Rows.Add(dr);         
       //Store the datatable value in session
        Session["dt"] = dt;
        //storing total rows count value
        Session["count"] = dt.Rows.Count;              
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
  

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;
        GridView1.DataBind();
  

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.