How To Save DataTable Into ViewState and Bind Gridview Without DataBase Using ASP.Net
I.Introduction
here i will explain how to store data into (programmatically) datatable and bind data into gridview in asp.net using C#.
II.Description:
To implement dynamically datatable we need to create one new datatable after that add columns and bind data to columns after that add those columns to datatable followed by the below shown code.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Name :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Department :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtDept" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Salary :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblerror" runat="server" Text="" ForeColor="Red" Visible="false"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
</td>
<td>
<asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
</td>
</tr>
</table>
<br />
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</form>
</body>
</html>
After completion of aspx page add following namespaces in codebehind
C# code
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
After completion of adding namespaces you need to write the code like as shown below
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable data = new DataTable();
data.Columns.Add("Name");
data.Columns.Add("Department");
data.Columns.Add("Salary");
ViewState["dt"] = data;
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
DataTable dt = ViewState["dt"] as DataTable;
DataRow drw= dt.NewRow();
drw["Name"] = txtName.Text;
drw["Department"] = txtDept.Text;
drw["Salary"] = txtSalary.Text;
dt.Rows.Add(drw);
ViewState["dt"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
lblerror.Visible = true;
lblerror.Text = "Successfully Saved !!!";
}
protected void clearControls()
{
txtName.Text = "";
txtDept.Text = "";
txtSalary.Text = "";
}
protected void btnClear_Click(object sender, EventArgs e)
{
clearControls();
lblerror.Visible = false;
}
Demo:
Hope you all understand the creation of Dynamic Datatable and bind to gridview in Asp.Net ..
Do have any query regarding my code you can comment below !!!
No comments:
Post a Comment