Saving Data From Gridview Footer In ASP.NET

Saving data from Gridview Footer in ASP
 
GridView control is the most important data control provided by ASP.NET. The GridView control enables you to connect to a datasource and display data in tabular format. How ever we can customize GridView, we can insert different controls in it and show to the user. It is a very user friendly controls. I think till now whatever project i have  worked,  I used this control. Now various jQuery grids are available which are used in different projects, but the flexibility  and coding standard of Gridview in ASP project  has no comparison with other controls.
 
Here, I am explaining one example which i used in various projects. I have also explained how to save data from GridView footer using TextBoxes.

I have added a GridView in my project and in command field added EDIT button like the following .

 

Now after adding it will look like the following in the design view. 

 

Now to add textboxes in footer go to edit template in the smart tag.

After that go to each property's  Footer field and from tool box drag and drop a textbox like the following:

 
Here i have added for the "Name" property. Similarly for every property I can start adding textbox in footer.

If it need other controls such as, dropdownlist I can add it also.

After adding all the textboxes add a BUTTON  to add the records.

 

Go to there property and change there id. Here i have shown how i changed the id of mobile textbox.
 
  
 
Now go to the "Edit columns" and convert all fields to template fields.
 
 
After that the source code will look like the following: 
  1. <div style="margin-left:15%">  
  2.     <asp:GridView ID="grd_details" runat="server" AutoGenerateColumns="False" ForeColor="#000099" OnRowEditing="grd_details_RowEditing" ShowFooter="True" Width="800px">  
  3.         <Columns>  
  4.             <asp:TemplateField HeaderText="Name" SortExpression="Name">  
  5.                 <EditItemTemplate>  
  6.                     <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>  
  7.                 </EditItemTemplate>  
  8.                 <FooterTemplate>  
  9.                     <asp:TextBox ID="txt_name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>  
  10.                 </FooterTemplate>  
  11.                 <ItemTemplate>  
  12.                     <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>  
  13.                 </ItemTemplate>  
  14.             </asp:TemplateField>  
  15.             <asp:TemplateField HeaderText="Email Id" SortExpression="Email">  
  16.                 <EditItemTemplate>  
  17.                     <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>  
  18.                 </EditItemTemplate>  
  19.                 <FooterTemplate>  
  20.                     <asp:TextBox ID="txt_email" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>  
  21.                 </FooterTemplate>  
  22.                 <ItemTemplate>  
  23.                     <asp:Label ID="Label3" runat="server" Text='<%# Bind("Email") %>'></asp:Label>  
  24.                 </ItemTemplate>  
  25.             </asp:TemplateField>  
  26.             <asp:TemplateField HeaderText="State" SortExpression="State">  
  27.                 <EditItemTemplate>  
  28.                     <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("State") %>'></asp:TextBox>  
  29.                 </EditItemTemplate>  
  30.                 <FooterTemplate>  
  31.                     <asp:TextBox ID="txt_state" runat="server" Text='<%# Bind("State") %>'></asp:TextBox>  
  32.                 </FooterTemplate>  
  33.                 <ItemTemplate>  
  34.                     <asp:Label ID="Label4" runat="server" Text='<%# Bind("State") %>'></asp:Label>  
  35.                 </ItemTemplate>  
  36.             </asp:TemplateField>  
  37.             <asp:TemplateField HeaderText="MOBILE" SortExpression="mobile">  
  38.                 <ItemTemplate>  
  39.                     <asp:Label ID="Label1" runat="server" Text='<%# Bind("mobileNo") %>'></asp:Label>  
  40.                 </ItemTemplate>  
  41.                 <EditItemTemplate>  
  42.                     <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("mobile") %>'></asp:TextBox>  
  43.                 </EditItemTemplate>  
  44.                 <FooterTemplate>  
  45.                     <asp:TextBox ID="txt_mobile" runat="server" Text='<%# Bind("mobile") %>'></asp:TextBox>  
  46.                 </FooterTemplate>  
  47.             </asp:TemplateField>  
  48.             <asp:TemplateField HeaderText="EDIT">  
  49.                 <EditItemTemplate>  
  50.                     <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />    
  51.                     <asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </EditItemTemplate>  
  52.                 <FooterTemplate>  
  53.                     <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update" OnClick="Button1_Click" Text="Add" /> </FooterTemplate>  
  54.                 <ItemTemplate>  
  55.                     <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" /> </ItemTemplate>  
  56.             </asp:TemplateField>  
  57.         </Columns>  
  58.         <FooterStyle BackColor="#66FFFF" />  
  59.         <HeaderStyle BackColor="Yellow" /> </asp:GridView>  
  60. </div>  
This will look  as follows in the design view.
 
 

Now add the following code to populate data from a table in the Gridview. 
  1. public void filldata()  
  2. {  
  3.    da = new SqlDataAdapter("select * from tbl_registration", BLL.connect());  
  4.    dt = new DataTable();  
  5.    da.fill(dt);  
  6.    grd_details.DataSource = dt;  
  7.    grd_details.DataBind();  
  8. }  
Now i am calling this method from pageload event. 
  1. public partial class _Default : System.Web.UI.Page  
  2. {  
  3.    SqlConnection con = new SqlConnection("Data Source=DEBENDRA;Initial Catalog=Students;User ID=sa;Password=123");  
  4.    SqlDataAdapter da;  
  5.    DataTable dt;  
  6.    protected void Page_Load(object sender, EventArgs e)  
  7.    {  
  8.       if(!IsPostBack)  
  9.       {  
  10.          filldata();  
  11.       }  
  12. }  
 
Here i have data in my table so it is binding my gridview, but when there is no data in my table my gridview will not be visible in the page. But i want my gridview in the page so for this i just modified my filldata method as in the following: 
  1. public void filldata()  
  2. {  
  3.     da = new SqlDataAdapter("select * from tbl_registration", BLL.connect());  
  4.     dt = new DataTable();  
  5.     da.Fill(dt);  
  6.     if (dt.Rows.Count > 0)  
  7.     {  
  8.         grd_details.DataSource = dt;  
  9.         grd_details.DataBind();  
  10.     }  
  11.     else  
  12.     {  
  13.         dt.Rows.Add(dt.NewRow());  
  14.         grd_details.DataSource = dt;  
  15.         grd_details.DataBind();  
  16.         int totalcolums = grd_details.Rows[0].Cells.Count;  
  17.         grd_details.Rows[0].Cells.Clear();  
  18.         grd_details.Rows[0].Cells.Add(new TableCell());  
  19.         grd_details.Rows[0].Cells[0].ColumnSpan = totalcolums;  
  20.         grd_details.Rows[0].Cells[0].Text = "No Data Found";  
  21.     }  
  22. }  
It will show my gridview in  the page even if there is no data to bind in the table. 
Enter detail
Now for saving data from the footer textbox write the following code. 
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     int result = 0;  
  4.     try  
  5.     {  
  6.         TextBox txtName = (TextBox) grd_details.FooterRow.FindControl("txt_name");  
  7.         TextBox TxtEmail = (TextBox) grd_details.FooterRow.FindControl("txt_email");  
  8.         TextBox Txtstate = (TextBox) grd_details.FooterRow.FindControl("txt_state");  
  9.         TextBox Txtmobile = (TextBox) grd_details.FooterRow.FindControl("txt_mobile");  
  10.         SqlCommand cmd = new SqlCommand("insert into tbl_registration(Name,Email,State,MobileNo) values('" + txtName.Text + "','" + TxtEmail.Text + "','" + Txtstate.Text + "','" + Txtmobile.Text + "')", con);  
  11.         con.Open();  
  12.         result = cmd.ExecuteNonQuery();  
  13.         con.Close();  
  14.         if (result == 1)  
  15.         {  
  16.             Response.Write("Data Saved");  
  17.             filldata();  
  18.         }  
  19.         else  
  20.         {  
  21.             Response.Write("Data Not saved.Please try again.");  
  22.         }  
  23.     }  
  24.     catch (Exception ex)  
  25.     {  
  26.         throw ex;  
  27.     }  
  28. }  
So in this way we can add data from GridView footer using TextBox.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now