By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,393 Members | 1,470 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Fill the GridView without any Database

P: 85
Hi guys, here i will explain how to fill the grid view without any database i.e. we will fill the grid view with the values of the text boxes and the drop down lists entered and selected by the user at run time, consider a simple scenario where user will enter the Reference, select the level of the reference e.g Low, Medium or High, enter the Organization Name and the contact details. and these values will be shown in the GridView.
Just copy and paste the below HTML and the code and you will see how it works.
the HTML is

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.     <title></title>
  7.  
  8.     <script type="text/javascript">
  9.         function Validation() {
  10.             var txtReference = document.getElementById("<%=txtReference.ClientID %>").value;
  11.             var ddllevelRef = document.getElementById("<%=ddllevelRef.ClientID %>").value;
  12.             var txtOrganizationName = document.getElementById("<%=txtOrganizationName.ClientID %>").value;
  13.             var txtContactDetails = document.getElementById("<%=txtContactDetails.ClientID %>").value;
  14.             if (txtReference == "") {
  15.                 alert("Please Enter the Reference:");
  16.                 return false;
  17.             }
  18.             else if (ddllevelRef == "0") {
  19.                 alert("Please Select Your Level:");
  20.                 return false;
  21.             }
  22.             else if (txtOrganizationName == "") {
  23.                 alert("Please Enter the Organization Name:");
  24.                 return false;
  25.             }
  26.             else if (txtContactDetails == "") {
  27.                 alert("Please Enter the Contact Details :");
  28.                 return false;
  29.             }
  30.             else {
  31.                 return true;
  32.             }
  33.         }
  34.  
  35.     </script>
  36.  
  37. </head>
  38. <body>
  39.     <form id="form1" runat="server">
  40.     <div>
  41.         <table>
  42.             <tr>
  43.                 <td>
  44.                     <table width="100%" border="0" cellspacing="8" cellpadding="0">
  45.                         <tr>
  46.                             <td>
  47.                                 <label>
  48.                                     References</label>
  49.                                 <br />
  50.                                 <asp:TextBox ID="txtReference" runat="server"></asp:TextBox>
  51.                             </td>
  52.                             <td>
  53.                                 <label>
  54.                                     Level
  55.                                 </label>
  56.                                 <br />
  57.                                 <asp:DropDownList ID="ddllevelRef" runat="server">
  58.                                     <asp:ListItem Selected="True" Value="0">-- Select One --</asp:ListItem>
  59.                                     <asp:ListItem Value="L">Low</asp:ListItem>
  60.                                     <asp:ListItem Value="M">Medium</asp:ListItem>
  61.                                     <asp:ListItem Value="H">High</asp:ListItem>
  62.                                 </asp:DropDownList>
  63.                             </td>
  64.                             <td>
  65.                                 <label>
  66.                                     Organization Name
  67.                                 </label>
  68.                                 <br />
  69.                                 <asp:TextBox ID="txtOrganizationName" runat="server"></asp:TextBox>
  70.                                 <table>
  71.                                     <tr>
  72.                                     </tr>
  73.                                 </table>
  74.                             </td>
  75.                             <td>
  76.                                 <label>
  77.                                     Contact Details</label>
  78.                                 <br />
  79.                                 <asp:TextBox ID="txtContactDetails" runat="server"></asp:TextBox>
  80.                             </td>
  81.                             <td width="40">
  82.                                 <asp:Button ID="btnSave" runat="server" OnClientClick="return Validation()" OnClick="lnkSaveReference_Click"
  83.                                     Text="Add" />
  84.                             </td>
  85.                             <td>
  86.                                 <asp:Button ID="btnCancel" runat="server" OnClick="lnkCancelReference_Click" Text="Cancel" />
  87.                             </td>
  88.                         </tr>
  89.                     </table>
  90.                     <hr />
  91.                     <table>
  92.                         <tr>
  93.                             <td>
  94.                                 <asp:GridView ID="gvReference" runat="server" HorizontalAlign="Left" PagerSettings-Visible="true"
  95.                                     CellPadding="4" AllowPaging="false" Width="645px">
  96.                                 </asp:GridView>
  97.                             </td>
  98.                         </tr>
  99.                     </table>
  100.                 </td>
  101.             </tr>
  102.         </table>
  103.     </div>
  104.     </form>
  105. </body>
  106. </html>
  107.  
  108.  
And the code in C# is as follows

Expand|Select|Wrap|Line Numbers
  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;
  8.  
  9. public partial class _Default : System.Web.UI.Page 
  10. {
  11.     protected void Page_Load(object sender, EventArgs e)
  12.     {
  13.  
  14.     }
  15.     protected void lnkSaveReference_Click(object sender, EventArgs e)
  16.     {
  17.  
  18.  
  19.             if (ViewState["CurrentDataReference"] != null)
  20.             {
  21.                 DataTable dt = (DataTable)ViewState["CurrentDataReference"];
  22.                 int count = dt.Rows.Count;
  23.                 BindGridReference(count);
  24.                 txtReference.Text = "";
  25.  
  26.                 ddllevelRef.SelectedValue = "0";
  27.                 txtOrganizationName.Text = "";
  28.                 txtContactDetails.Text = "";
  29.             }
  30.             else
  31.             {
  32.                 BindGridReference(1);
  33.                 txtReference.Text = "";
  34.  
  35.                 ddllevelRef.SelectedValue = "0";
  36.                 txtOrganizationName.Text = "";
  37.                 txtContactDetails.Text = "";
  38.             }
  39.  
  40.  
  41.     }
  42.     private void BindGridReference(int rowcount)
  43.     {
  44.         DataTable Dt = new DataTable();
  45.         DataRow dr;
  46.         Dt.Columns.Add(new System.Data.DataColumn("References", typeof(String)));
  47.         Dt.Columns.Add(new System.Data.DataColumn("Level", typeof(String)));
  48.         Dt.Columns.Add(new System.Data.DataColumn("Organization Name", typeof(String)));
  49.         Dt.Columns.Add(new System.Data.DataColumn("Contact Details", typeof(String)));
  50.  
  51.         if (ViewState["CurrentDataReference"] != null)
  52.         {
  53.             for (int i = 0; i < rowcount + 1; i++)
  54.             {
  55.                 Dt = (DataTable)ViewState["CurrentDataReference"];
  56.                 if (Dt.Rows.Count > 0)
  57.                 {
  58.                     dr = Dt.NewRow();
  59.                     dr[0] = Dt.Rows[0][0].ToString();
  60.  
  61.                 }
  62.             }
  63.             dr = Dt.NewRow();
  64.             dr[0] = txtReference.Text.Trim();
  65.  
  66.             dr[1] = ddllevelRef.SelectedItem.Text;
  67.             dr[2] = txtOrganizationName.Text.Trim();
  68.             dr[3] = txtContactDetails.Text.Trim();
  69.             Dt.Rows.Add(dr);
  70.         }
  71.         else
  72.         {
  73.             dr = Dt.NewRow();
  74.             dr[0] = txtReference.Text.Trim(); 
  75.             dr[1] = ddllevelRef.SelectedItem.Text;
  76.             dr[2] = txtOrganizationName.Text.Trim();
  77.             dr[3] = txtContactDetails.Text.Trim();
  78.             Dt.Rows.Add(dr);
  79.         }
  80.  
  81.  
  82.         if (ViewState["CurrentDataReference"] != null)
  83.         {
  84.             gvReference.DataSource = (DataTable)ViewState["CurrentDataReference"];
  85.             gvReference.DataBind();
  86.             gvReference.Focus();
  87.         }
  88.         else
  89.         {
  90.             gvReference.DataSource = Dt;
  91.             gvReference.DataBind();
  92.             gvReference.Focus();
  93.  
  94.         }
  95.         ViewState["CurrentDataReference"] = Dt;
  96.     }
  97.     protected void lnkCancelReference_Click(object sender, EventArgs e)
  98.     {
  99.         txtReference.Text = "";
  100.  
  101.         ddllevelRef.SelectedValue = "0";
  102.         txtOrganizationName.Text = "";
  103.         txtContactDetails.Text = "";
  104.         gvReference.DataSource = null;
  105.         gvReference.DataBind();
  106.  
  107.     }
  108. }
  109.  
  110.  

Addan
Aug 3 '12 #1
Share this Article
Share on Google+