Click here to Skip to main content
15,031,361 members
Please Sign up or sign in to vote.
4.71/5 (3 votes)
See more:
XML
<asp:GridView ID="showinfogrid" runat="server" AutoGenerateColumns="False"
    Width="100%">
           <Columns>
            <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"/>
           </Columns>
       </asp:GridView>

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

}

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.
Posted
Updated 4-Dec-13 21:50pm
v3

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.
   
v2
Check up this tutorial:

http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx[^]

To prevent page from being refreshed, add the gridview into UpdatePanel control.

http://msdn.microsoft.com/en-us/library/bb399001.aspx[^]
   
v4
Use the below code to add the new row in gridview.


C#
//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";
   
Comments
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.
Hi

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


sample code:


XML
<asp:UpdatePanel ID="up" runat="server">
        <ContentTemplate>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:GridView ID="showinfogrid" runat="server" AutoGenerateColumns="False" Width="100%">
                <Columns>
                    <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" />
                </Columns>
            </asp:GridView>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>
   
Comments
Thanks7872 5-Dec-13 3:52am
   
+2.

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

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