Reputation: 860
I have two text boxes and a button.In the buttons click event I add the values captured in the text boxes as a new row in my grdiview. Now what I want is when my page loads again my gridview will show the existing data which I add in it and append new record without using database.
Here is my code:
private void BindGrid(int rowcount)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("First Name", typeof(String));
dt.Columns.Add("Last Name", typeof(String));
if (ViewState["CurrentData"] != null)
{
for (int i = 0; i < rowcount + 1; i++)
{
dt = (DataTable)ViewState["CurrentData"];
if (dt.Rows.Count > 0)
{
dr = dt.NewRow();
dr[0] = dt.Rows[0][0].ToString();
}
}
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dr[1] = TextBox2.Text;
dt.Rows.Add(dr);
}
else
{
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dr[1] = TextBox2.Text;
dt.Rows.Add(dr);
}
// If ViewState has a data then use the value as the DataSource
if (ViewState["CurrentData"] != null)
{
GridView1.DataSource = (DataTable)ViewState["CurrentData"];
GridView1.DataBind();
}
else
{
// Bind GridView with the initial data assocaited in the DataTable
GridView1.DataSource = dt;
GridView1.DataBind();
}
// Store the DataTable in ViewState to retain the values
ViewState["CurrentData"] = dt;
}
Button click event:
protected void Button1_Click(object sender, EventArgs e)
{
// Check if the ViewState has a data assoiciated within it. If
if (ViewState["CurrentData"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentData"];
int count = dt.Rows.Count;
BindGrid(count);
}
else
{
BindGrid(1);
}
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
TextBox1.Focus();
}
This is my ASPX page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"/>
<asp:TextBox ID="TextBox2" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" > <AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
</form>
</body>
</html>
Upvotes: 0
Views: 8834
Reputation: 17614
Here is a code which should work for you
private void BindGrid()
{
GridView1.DataSource = GetDataTable();
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
DataRow dr;
DataTable dt = GetDataTable();
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dr[1] = TextBox2.Text;
dt.Rows.Add(dr);
ViewState["CurrentData"] = dt;
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
TextBox1.Focus();
//**updated**
BindGrid();
}
protected DataTable GetDataTable()
{
DataTable dt;
if (ViewState["CurrentData"] != null)
{
dt = (DataTable)ViewState["CurrentData"];
}
else
{
dt = new DataTable();
dt.Columns.Add("First Name", typeof(String));
dt.Columns.Add("Last Name", typeof(String));
//**Update**/
ViewState["CurrentData"] = dt;
}
return dt;
}
and call the function BindGrid
on page load.
if you want it after visiting another page then you should use session
rather than viewstate
Change the functions accordingly
protected DataTable GetDataTable()
{
DataTable dt;
if (Session["CurrentData"] != null)
{
dt = (DataTable)Session["CurrentData"];
}
else
{
dt = new DataTable();
dt.Columns.Add("First Name", typeof(String));
dt.Columns.Add("Last Name", typeof(String));
Session["CurrentData"] = dt;
}
return dt;
}
Upvotes: 2