Fill the GridView without any Database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mudassir
    New Member
    • May 2012
    • 85

    Fill the GridView without any Database

    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

    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    
        <script type="text/javascript">
            function Validation() {
                var txtReference = document.getElementById("<%=txtReference.ClientID %>").value;
                var ddllevelRef = document.getElementById("<%=ddllevelRef.ClientID %>").value;
                var txtOrganizationName = document.getElementById("<%=txtOrganizationName.ClientID %>").value;
                var txtContactDetails = document.getElementById("<%=txtContactDetails.ClientID %>").value;
                if (txtReference == "") {
                    alert("Please Enter the Reference:");
                    return false;
                }
                else if (ddllevelRef == "0") {
                    alert("Please Select Your Level:");
                    return false;
                }
                else if (txtOrganizationName == "") {
                    alert("Please Enter the Organization Name:");
                    return false;
                }
                else if (txtContactDetails == "") {
                    alert("Please Enter the Contact Details :");
                    return false;
                }
                else {
                    return true;
                }
            }
    
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                        <table width="100%" border="0" cellspacing="8" cellpadding="0">
                            <tr>
                                <td>
                                    <label>
                                        References</label>
                                    <br />
                                    <asp:TextBox ID="txtReference" runat="server"></asp:TextBox>
                                </td>
                                <td>
                                    <label>
                                        Level
                                    </label>
                                    <br />
                                    <asp:DropDownList ID="ddllevelRef" runat="server">
                                        <asp:ListItem Selected="True" Value="0">-- Select One --</asp:ListItem>
                                        <asp:ListItem Value="L">Low</asp:ListItem>
                                        <asp:ListItem Value="M">Medium</asp:ListItem>
                                        <asp:ListItem Value="H">High</asp:ListItem>
                                    </asp:DropDownList>
                                </td>
                                <td>
                                    <label>
                                        Organization Name
                                    </label>
                                    <br />
                                    <asp:TextBox ID="txtOrganizationName" runat="server"></asp:TextBox>
                                    <table>
                                        <tr>
                                        </tr>
                                    </table>
                                </td>
                                <td>
                                    <label>
                                        Contact Details</label>
                                    <br />
                                    <asp:TextBox ID="txtContactDetails" runat="server"></asp:TextBox>
                                </td>
                                <td width="40">
                                    <asp:Button ID="btnSave" runat="server" OnClientClick="return Validation()" OnClick="lnkSaveReference_Click"
                                        Text="Add" />
                                </td>
                                <td>
                                    <asp:Button ID="btnCancel" runat="server" OnClick="lnkCancelReference_Click" Text="Cancel" />
                                </td>
                            </tr>
                        </table>
                        <hr />
                        <table>
                            <tr>
                                <td>
                                    <asp:GridView ID="gvReference" runat="server" HorizontalAlign="Left" PagerSettings-Visible="true"
                                        CellPadding="4" AllowPaging="false" Width="645px">
                                    </asp:GridView>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
        </form>
    </body>
    </html>
    And the code in C# is as follows

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void lnkSaveReference_Click(object sender, EventArgs e)
        {
           
            
                if (ViewState["CurrentDataReference"] != null)
                {
                    DataTable dt = (DataTable)ViewState["CurrentDataReference"];
                    int count = dt.Rows.Count;
                    BindGridReference(count);
                    txtReference.Text = "";
                  
                    ddllevelRef.SelectedValue = "0";
                    txtOrganizationName.Text = "";
                    txtContactDetails.Text = "";
                }
                else
                {
                    BindGridReference(1);
                    txtReference.Text = "";
                  
                    ddllevelRef.SelectedValue = "0";
                    txtOrganizationName.Text = "";
                    txtContactDetails.Text = "";
                }
           
            
        }
        private void BindGridReference(int rowcount)
        {
            DataTable Dt = new DataTable();
            DataRow dr;
            Dt.Columns.Add(new System.Data.DataColumn("References", typeof(String)));
            Dt.Columns.Add(new System.Data.DataColumn("Level", typeof(String)));
            Dt.Columns.Add(new System.Data.DataColumn("Organization Name", typeof(String)));
            Dt.Columns.Add(new System.Data.DataColumn("Contact Details", typeof(String)));
    
            if (ViewState["CurrentDataReference"] != null)
            {
                for (int i = 0; i < rowcount + 1; i++)
                {
                    Dt = (DataTable)ViewState["CurrentDataReference"];
                    if (Dt.Rows.Count > 0)
                    {
                        dr = Dt.NewRow();
                        dr[0] = Dt.Rows[0][0].ToString();
                      
                    }
                }
                dr = Dt.NewRow();
                dr[0] = txtReference.Text.Trim();
               
                dr[1] = ddllevelRef.SelectedItem.Text;
                dr[2] = txtOrganizationName.Text.Trim();
                dr[3] = txtContactDetails.Text.Trim();
                Dt.Rows.Add(dr);
            }
            else
            {
                dr = Dt.NewRow();
                dr[0] = txtReference.Text.Trim(); 
                dr[1] = ddllevelRef.SelectedItem.Text;
                dr[2] = txtOrganizationName.Text.Trim();
                dr[3] = txtContactDetails.Text.Trim();
                Dt.Rows.Add(dr);
            }
    
         
            if (ViewState["CurrentDataReference"] != null)
            {
                gvReference.DataSource = (DataTable)ViewState["CurrentDataReference"];
                gvReference.DataBind();
                gvReference.Focus();
            }
            else
            {
                gvReference.DataSource = Dt;
                gvReference.DataBind();
                gvReference.Focus();
    
            }
            ViewState["CurrentDataReference"] = Dt;
        }
        protected void lnkCancelReference_Click(object sender, EventArgs e)
        {
            txtReference.Text = "";
           
            ddllevelRef.SelectedValue = "0";
            txtOrganizationName.Text = "";
            txtContactDetails.Text = "";
            gvReference.DataSource = null;
            gvReference.DataBind();
    
        }
    }

    Addan
    Last edited by Frinavale; Aug 3 '12, 01:06 PM.
Working...