Hello,
If you want to add new row in grid view at run time using ASP.NET(C# ) then firstly you need to have labels, textbox in your grid view.
Here is the image of that grid view on which we are going to work...
The Source code for creating grid view is like....
Now, When you fill the text box for name and salary....and click on the insert button like......
The code to insert the value in grid view..
The final image after inserting the values in grid view is....
If you want to add new row in grid view at run time using ASP.NET(C# ) then firstly you need to have labels, textbox in your grid view.
Here is the image of that grid view on which we are going to work...
The Source code for creating grid view is like....
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
onpageindexchanging="GridView1_PageIndexChanging"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" onsorting="GridView1_Sorting"
PageSize="5" ShowFooter="True" ForeColor="Black" GridLines="Vertical">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<EditItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NAME" SortExpression="NAME">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SALARY" SortExpression="SALARY">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("salary") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("salary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
Now, When you fill the text box for name and salary....and click on the insert button like......
The code to insert the value in grid view..
protected void Insert_Click(object sender, EventArgs e)
{
con.Open();
TextBox empNAME = (TextBox)GridView1.FooterRow.FindControl("TextBox4");
TextBox empSALARY = (TextBox)GridView1.FooterRow.FindControl("TextBox5");
String insertquery="insert into emp values('"+empNAME.Text+"','"+empSALARY.Text+"')";
SqlCommand cmd = new SqlCommand(insertquery, con);
Response.Write("1 Row has been inserted. ");
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
BindData();
}
{
con.Open();
TextBox empNAME = (TextBox)GridView1.FooterRow.FindControl("TextBox4");
TextBox empSALARY = (TextBox)GridView1.FooterRow.FindControl("TextBox5");
String insertquery="insert into emp values('"+empNAME.Text+"','"+empSALARY.Text+"')";
SqlCommand cmd = new SqlCommand(insertquery, con);
Response.Write("1 Row has been inserted. ");
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
BindData();
}
The final image after inserting the values in grid view is....