How to add a new row in the GridView control by using the GridView Footer Template

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.

<FooterTemplate>
<asp:dropdownlist ID="add_aname" DataSourceID="ObjectDataSource1" 
DataTextField ="AccountName" runat="server">
</asp:DropDownList>
</FooterTemplate>

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[0].
    			FindControl("add_aname");
	int sdf = DDB.SelectedIndex;
	if (txt1.Text != "")
	{	
		rw[0] = DDB.SelectedValue;
         dt.Rows.Add(rw);
}}

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#.