Click here to Skip to main content
15,945,939 members
Please Sign up or sign in to vote.
4.71/5 (3 votes)
See more:
<asp:GridView ID="showinfogrid" runat="server" AutoGenerateColumns="False"
            <asp:BoundField DataField ="Name" HeaderText ="Employee_Name" ReadOnly="true" />
            <asp:BoundField DataField ="Employee_ID" HeaderText ="Emp_ID" ReadOnly="true"/>
            <asp:BoundField DataField ="Department" HeaderText ="Department" ReadOnly="true"/>
            <asp:BoundField DataField ="Designation" HeaderText ="Designation" ReadOnly="true"/>

this is the click event in the button
protected void Button1_Click(object sender, EventArgs e)
    desLabel.Text = Desigdrop.SelectedItem.Text;
    DataTable dt = new DataTable();
    this.showinfogrid.Visible = true;
    dt.Rows.Add(nameTextBox1.Text.Trim(), empidTextBox2.Text.Trim(), Deptdrop.SelectedItem.Text, Desigdrop.SelectedItem.Text);
    showinfogrid.DataSource = dt;


Anyone tell me how i add row each time in the grid view when i click button without refreshing the page but the new row add after the previous row.
Updated 4-Dec-13 21:50pm

See this link : Dynamically adding and deleting rows from ASP.NET GridView[^]. It is all about what you are looking for.

Further,as suggested by Karthik[^],wrap your code inside update panel.
Share this answer
Check up this tutorial:[^]

To prevent page from being refreshed, add the gridview into UpdatePanel control.[^]
Share this answer

Put all your controls in an ajax update panel and try it..

sample code:

<asp:UpdatePanel ID="up" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            <asp:GridView ID="showinfogrid" runat="server" AutoGenerateColumns="False" Width="100%">
                    <asp:BoundField DataField="Name" HeaderText="Employee_Name" ReadOnly="true" />
                    <asp:BoundField DataField="Employee_ID" HeaderText="Emp_ID" ReadOnly="true" />
                    <asp:BoundField DataField="Department" HeaderText="Department" ReadOnly="true" />
                    <asp:BoundField DataField="Designation" HeaderText="Designation" ReadOnly="true" />
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Share this answer
Thanks7872 5-Dec-13 3:52am    

Actual problem is with adding rows,solution is not complete.
Karthik_Mahalingam 5-Dec-13 4:17am    
i got it rohan
Kawshik_itbd 6-Dec-13 1:43am    
it works but can u help how i add row one after one when i click button ,previous row can not be remove only add new row
Use the below code to add the new row in gridview.

//showinfogrid.Rows[0].Cells.Clear();//clear the gird data
showinfogrid.Rows[0].Cells.Add(new TableCell()); 
showinfogrid.Rows[0].Cells[0].ColumnSpan = TotalColumns;// to merge
showinfogrid.Rows[0].Cells[0].Text = "No Record Found";
Share this answer
Kawshik_itbd 5-Dec-13 3:42am    
where i use this code plz tell me ?row is added in the gridview when i click and the previous row must aviable don;t remove
Ganesh Raja 5-Dec-13 5:34am    
u can add the that code in "Button1_Click" event.

Note: If the data is available, add that code after binding or if the data not available add this code after adding the columns.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900