Dynamic Gridview Bind without Database in asp.net c#
Many times we want to save the values in dynamic way
means we do not want to save the values in data base .so I this code I have
bind the grid view with Data table.
Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamicallygridviewwithoutdatabase.aspx.cs" Inherits="dynamicallygridviewwithoutdatabase" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width:350px;">
<legend>Dynamic Gridview Bind without Database
</legend>
EmpID<br />
<asp:TextBox ID="txtid" runat="server"></asp:TextBox>
<br />
Name
<br /><asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
Contact
<br />
<asp:TextBox ID="txtcontact" runat="server"></asp:TextBox><br />
Email <br />
<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
<br />
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</fieldset>
</div>
</form>
</body>
</html>
Code behind (C#)
using
System;
using System.Collections.Generic;
using
System.Data;
using
System.IO;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
public partial class dynamicallygridviewwithoutdatabase : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
DataTable datatbl = new DataTable();
if (ViewState["Row"] != null)
{
datatbl
= (DataTable)ViewState["Row"];
DataRow dr = null;
if (datatbl.Rows.Count > 0)
{
dr =
datatbl.NewRow();
dr["empid"] = txtid.Text;
dr["name"] = txtname.Text;
dr["contact"] = txtcontact.Text;
dr["emailid"] = txtemail.Text;
datatbl.Rows.Add(dr);
ViewState["Row"] = datatbl;
GridView1.DataSource = ViewState["Row"];
GridView1.DataBind();
}
}
else
{
datatbl.Columns.Add("empid", typeof(int));
datatbl.Columns.Add(new DataColumn("name", typeof(string)));
datatbl.Columns.Add("contact", typeof(string));
datatbl.Columns.Add("emailid", typeof(string));
DataRow dr1 = datatbl.NewRow();
dr1 =
datatbl.NewRow();
dr1["empid"] = txtid.Text;
dr1["name"] = txtname.Text;
dr1["contact"] = txtcontact.Text;
dr1["emailid"] = txtemail.Text;
datatbl.Rows.Add(dr1);
ViewState["Row"] = datatbl;
GridView1.DataSource = ViewState["Row"];
GridView1.DataBind();
}
}
catch(Exception ex)
{
}
}
}
OUT-PUT:-
Dynamic Gridview Bind without Database in asp.net c#
Reviewed by NEERAJ SRIVASTAVA
on
1:29:00 PM
Rating:
No comments: