In this article we will see how to add a new row in the GridView control by using the GridView footer template. Steps to add a new row in the GridView control by using the GridView footer template are:
Declare <FooterTemplate> element within the TemplateField element which is used for adding a new record in the GridView control. Within the <FooterTemplate>, define the server control which is used to accept new data.
The below code demonstrates how to use <FooterTemplate> and how to add DropDownList in the <FooterTemplate> of the GridView control. The Grid View control displays DropDownList control for selecting new accounts.
<asp:dropdownlist ID="add_aname" DataSourceID="ObjectDataSource1"
DataTextField ="AccountName" runat="server">
After the above steps are completed, write the Event handler for the GridView RowCommand event. In the Event handler program the following.
Add a new row to the DataTable.
Retrieve the values of controls (DropDownList and TextBox controls) which are in the Footer Template.
Assign the values entered by the user to the row and assign the row to the DataTable.
protected void grdAccounts_RowCommand(object sender, GridViewCommandEventArgs e)
DataRow rw = dt.NewRow();
DropDownList DDB = (DropDownList)grdAccounts.FooterRow.Cells.
int sdf = DDB.SelectedIndex;
if (txt1.Text != "")
rw = DDB.SelectedValue;
For a detailed explanation of how to add a new row in the GridView control by using the GridView footer template, refer the title :Develop ERP software using ASP.Net 3.5 and C#.