Binding GridView without Database

Here the document explains that how to create the simple GridView and binding it with the sample data table in the application itself without using the database.

First, Create a sample user form and save the details those are entered in the form and the saved details should bind with the GridView. The source code as follows,

  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="userdata.aspx.cs"Inherits="WebApplication17.userdata"%>  
  2.   
  3.     <!DOCTYPEhtml>  
  4.   
  5.     <htmlxmlns="http://www.w3.org/1999/xhtml">  
  6.         <headrunat="server">  
  7.             <title></title>  
  8.             </head>  
  9.   
  10.             <body>  
  11.                 <formid="form1" runat="server">  
  12.                     <divalign="center">  
  13.   
  14.                         <table>  
  15.                             <tr>  
  16.                                 <tdcolspan="3">USER FORM</td>  
  17.                             </tr>  
  18.                         </table>  
  19.                         <table>  
  20.                             <tr>  
  21.                                 <td>  
  22.                                     <asp:LabelID="Label1" runat="server" Text="Enter UserName"></asp:Label>  
  23.                                 </td>  
  24.                                 <td>  
  25.                                     <asp:TextBoxID="TextBox1" runat="server"></asp:TextBox>  
  26.                                 </td>  
  27.                             </tr>  
  28.                             <tr>  
  29.                                 <td>  
  30.                                     <asp:LabelID="Label2" runat="server" Text="Enter Password"></asp:Label>  
  31.                                 </td>  
  32.                                 <td>  
  33.                                     <asp:TextBoxID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>  
  34.                                 </td>  
  35.                             </tr>  
  36.                             <tr>  
  37.                                 <td>  
  38.                                     <asp:LabelID="Label3" runat="server" Text="Confirm Password"></asp:Label>  
  39.                                 </td>  
  40.                                 <td>  
  41.                                     <asp:TextBoxID="TextBox3" runat="server" TextMode="Password"></asp:TextBox>  
  42.                                 </td>  
  43.                             </tr>  
  44.                             <tr>  
  45.                                 <td>  
  46.                                     <asp:LabelID="Label4" runat="server" Text="Email ID"></asp:Label>  
  47.                                 </td>  
  48.                                 <td>  
  49.                                     <asp:TextBoxID="TextBox4" runat="server"></asp:TextBox>  
  50.                                 </td>  
  51.                             </tr>  
  52.                             <tr>  
  53.                                 <td>  
  54.                                     <asp:LabelID="Label5" runat="server" Text="Address"></asp:Label>  
  55.                                 </td>  
  56.                                 <td>  
  57.                                     <asp:TextBoxID="TextBox5" runat="server"></asp:TextBox>  
  58.                                 </td>  
  59.                             </tr>  
  60.                             <tr>  
  61.                                 <td>  
  62.                                     <asp:LabelID="Label6" runat="server" Text="Contact Number"></asp:Label>  
  63.                                 </td>  
  64.                                 <td>  
  65.                                     <asp:TextBoxID="TextBox6" runat="server"></asp:TextBox>  
  66.                                 </td>  
  67.                             </tr>  
  68.                             <tr>  
  69.                                 <td>  
  70.                                     <asp:LabelID="Label7" runat="server" Text="Gender"></asp:Label>  
  71.                                 </td>  
  72.                                 <td>  
  73.                                     <asp:RadioButtonListID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" Width="134px">  
  74.                                         <asp:ListItem>Male</asp:ListItem>  
  75.                                         <asp:ListItem>Female</asp:ListItem>  
  76.                                         </asp:RadioButtonList>  
  77.                                 </td>  
  78.                             </tr>  
  79.                         </table>  
  80.                         <table>  
  81.                             <tr>  
  82.                                 <td>  
  83.                                     <asp:ButtonID="Button1" runat="server" Text="Save Your Data" OnClick="Button1_Click" />  
  84.                                 </td>  
  85.                             </tr>  
  86.                         </table>  
  87.                         </div>  
  88.                         <divalign="center">  
  89.                             <asp:GridViewID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">  
  90.                                 <AlternatingRowStyleBackColor="White" />  
  91.                                 <EditRowStyleBackColor="#7C6F57" />  
  92.                                 <FooterStyleBackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
  93.                                 <HeaderStyleBackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
  94.                                 <PagerStyleBackColor="#666666" ForeColor="White" HorizontalAlign="Center" />  
  95.                                 <RowStyleBackColor="#E3EAEB" />  
  96.                                 <SelectedRowStyleBackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />  
  97.                                 <SortedAscendingCellStyleBackColor="#F8FAFA" />  
  98.                                 <SortedAscendingHeaderStyleBackColor="#246B61" />  
  99.                                 <SortedDescendingCellStyleBackColor="#D4DFE1" />  
  100.                                 <SortedDescendingHeaderStyleBackColor="#15524A" />  
  101.                                 </asp:GridView>  
  102.                                 <br/>  
  103.                                 </div>  
  104.                                 </form>  
  105.             </body>  
  106.   
  107.             </html>  
And coming to the Coding part, The Namespaces we need to add as,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. Using System.Web;  
  5. Using System.Web.UI;  
  6. Using System.Web.UI.WebControls;  
  7. Using System.Data;  
And when we click on the “SAVE YOUR BUTTON” it will automatically generate an event, in that event we need to write the code using the Data Table concept as follows,
  1. Protectedvoid Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     DataTabledt = newDataTable();  
  4.     dt.Columns.Add("Username"typeof(string));  
  5.     dt.Columns.Add("Password"typeof(string));  
  6.     dt.Columns.Add("Email ID"typeof(string));  
  7.     dt.Columns.Add("Address"typeof(string));  
  8.     dt.Columns.Add("Contact"typeof(string));  
  9.     dt.Columns.Add("Gender"typeof(string));  
  10.     If(TextBox2.Text == TextBox3.Text)   
  11.     {  
  12.         dt.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox4.Text, TextBox5.Text, RadioButtonList1.SelectedItem.Text);  
  13.   
  14.         GridView1.DataSource = dt;  
  15.         GridView1.DataBind();  
  16.     } else   
  17.     {  
  18.         Response.Write("<script>alert ('Password and ConformPassword are not matched') ;< /script>");  
  19.     }  
  20.   
  21. }  
Then it will check the Password and Confirm Password, If both are same then it will save the data and display data in GridView. If those are not same it will display a pop-up message to the user.