In this article I will explain with an example, how to bind (populate) GridView using Session data in ASP.Net using C# and VB.Net.
The concept is to create a dynamic DataTable, save it to Session and bind it to GridView.
Later whenever a new row is added, the DataTable is fetched from Session, it is updated and then again used to bind the GridView.
HTML Markup
The following HTML Markup consists of an ASP.Net GridView along with some TextBoxes and a Button in order to insert data in GridView control.
<asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false"
EmptyDataText="No records has been added.">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="120" />
</Columns>
</asp:GridView>
<br />
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
<tr>
<td style="padding-bottom: 10px">
Name:<br />
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td style="padding-bottom: 10px">
Country:<br />
<asp:TextBox ID="txtCountry" runat="server" />
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
</td>
</tr>
</table>
Namespace
You will need to import the following namespace.
C#
VB.Net
Binding the GridView using an Empty DataTable
Initially a DataTable has to be created and is saved in the Session variable and then the BindGrid method is executed which populates the GridView from the DataTable saved in Session variable.
Here the primary reason to use Session variable is to preserve the GridView data across PostBacks.
Note: You can also make use of ViewState variable for saving the DataTable.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] {new DataColumn("Name"), new DataColumn("Country") });
Session["Customers"] = dt;
this.BindGrid();
}
}
protected void BindGrid()
{
GridView1.DataSource = (DataTable)Session["Customers"];
GridView1.DataBind();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Name"), New DataColumn("Country")})
Session("Customers") = dt
Me.BindGrid()
End If
End Sub
Protected Sub BindGrid()
GridView1.DataSource = DirectCast(Session("Customers"), DataTable)
GridView1.DataBind()
End Sub
Bind (Populate) GridView using Session in ASP.Net
First the DataTable is fetched from the Session variable and then a new Row is added to the DataTable by making use of the data from the TextBoxes.
Finally the DataTable is saved back in Session variable and the BindGrid method is executed which updates the GridView data.
C#
protected void Insert(object sender, EventArgs e)
{
DataTable dt = (DataTable)Session["Customers"];
dt.Rows.Add(txtName.Text.Trim(), txtCountry.Text.Trim());
Session["Customers"] = dt;
this.BindGrid();
txtName.Text = string.Empty;
txtCountry.Text = string.Empty;
}
VB.Net
Protected Sub Insert(sender As Object, e As EventArgs)
Dim dt As DataTable = DirectCast(Session("Customers"), DataTable)
dt.Rows.Add(txtName.Text.Trim(), txtCountry.Text.Trim())
Session("Customers") = dt
Me.BindGrid()
txtName.Text = String.Empty
txtCountry.Text = String.Empty
End Sub
Screenshot
Demo
Downloads