Add Rows to Gridview Without Database and State Management Techniques

Here the GridView will be populated with data based on the values entered in the Textboxes on the Save Button click and retain the GridView data on post-back also.
  1. Go to Start, All Programs and open Microsoft Visual Studio 2013.

  2. Now, click File, then select New Project and click Visual C#. Then select ASP.NET Web Application, Empty and press OK.

  3. Provide the web application a name and location accordingly. I named my web application GridViewWithoutDBandStateMngmt.

  4. Now the project will open. Right-click on the web application name, add a New Item and select Web Form. Then click on Add.

  5. Drag and drop one button, three textboxes and one GridView control onto the WebForm1.aspx Page.
Now the WebForm1.aspx page will look like this in the Source View.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridViewWithoutDBandStateMngmt.WebForm1" %>        
  2.         
  3. <!DOCTYPE html>        
  4.         
  5. <html xmlns="http://www.w3.org/1999/xhtml">        
  6. <head runat="server">        
  7.     <title></title>        
  8. </head>        
  9. <body>        
  10.     <form id="form1" runat="server">        
  11.         <div>        
  12.             <table>        
  13.                 <tr>        
  14.                     <td>Student ID:</td>        
  15.                     <td>        
  16.                         <asp:TextBox ID="txtStID" runat="server"></asp:TextBox>        
  17.                     </td>        
  18.                 </tr>        
  19.                 <tr>        
  20.                     <td>Student Name:</td>        
  21.                     <td>        
  22.                         <asp:TextBox ID="txtStName" runat="server"></asp:TextBox>        
  23.                     </td>        
  24.                 </tr>        
  25.                 <tr>        
  26.                     <td>Student Class:</td>        
  27.                     <td>        
  28.                         <asp:TextBox ID="txtStClass" runat="server"></asp:TextBox>        
  29.                     </td>        
  30.                 </tr>        
  31.                 <tr>        
  32.                     <td colspan="2" align="center">        
  33.                         <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />        
  34.                     </td>        
  35.                 </tr>        
  36.             </table>        
  37.         </div>        
  38.         <div>        
  39.             <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"        
  40.                 GridLines="None">        
  41.                 <AlternatingRowStyle BackColor="White" ForeColor="#284775" />        
  42.                 <EditRowStyle BackColor="#999999" />        
  43.                 <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />        
  44.                 <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />        
  45.                 <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />        
  46.                 <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />        
  47.                 <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />        
  48.                 <SortedAscendingCellStyle BackColor="#E9E7E2" />        
  49.                 <SortedAscendingHeaderStyle BackColor="#506C8C" />        
  50.                 <SortedDescendingCellStyle BackColor="#FFFDF8" />        
  51.                 <SortedDescendingHeaderStyle BackColor="#6F8DAE" />        
  52.             </asp:GridView>        
  53.         </div>        
  54.     </form>        
  55. </body>        
  56. </html>
Now go to Design view and double-click on the Save Button and write the following code:
  1. protected void btnSave_Click(object sender, EventArgs e)    
  2. {    
  3.     if (txtStID.Text != string.Empty && txtStName.Text != string.Empty && txtStClass.Text != string.Empty)    
  4.     {    
  5.         DataTable dt = new DataTable();    
  6.         dt.Columns.Add("Student ID");    
  7.         dt.Columns.Add("Student Name");    
  8.         dt.Columns.Add("Student Class");    
  9.   
  10.         for (int row = 0; row < GridView1.Rows.Count; row++)    
  11.         {    
  12.             DataRow dr = dt.NewRow();    
  13.             dr[0] = GridView1.Rows[row].Cells[0].Text;    
  14.             dr[1] = GridView1.Rows[row].Cells[1].Text;    
  15.             dr[2] = GridView1.Rows[row].Cells[2].Text;    
  16.             dt.Rows.Add(dr);    
  17.         }    
  18.   
  19.         dt.Rows.Add(txtStID.Text, txtStName.Text, txtStClass.Text);    
  20.         GridView1.DataSource = dt;    
  21.         GridView1.DataBind();    
  22.   
  23.         txtStClass.Text = string.Empty;    
  24.         txtStID.Text = string.Empty;    
  25.         txtStName.Text = string.Empty;    
  26.     }    
  27. }
Now run the application, the page will look as in the following:
 
 
 
Now enter details into the textboxes and click on the Save Button, the page will look as in the following:
 
gridview
Enter the details into the textboxes and click the Save Button, the page will look as in the following:
 
 
Similar to the preceding you can add other records.


Similar Articles