In this article I will explain with an example, how to
DataTable as a temporary storage table in
ASP.Net for storing the records that we need for processing in
ViewState or Session variables.
The concept is to create a dynamic
DataTable, store some values in it and then keep the
DataTable either in
ViewState or Session depending on whether you want it only on the page or throughout the application respectively.
In order to illustrate this concept, I will make use an
ASP.Net GridView control on which I’ll perform
Edit and
Update operations and the save the updated data with the temporary
DataTable.
HTML Markup
The following
HTML Markup consists of:
GridView – For displaying data.
Columns
The
GridView consists of two
BoundField column and
TemplateField columns.
TemplateField – The TemplateField column consists of ItemTemplate and EditItemTemplate.
ItemTemplate – It consists of a LinkButton for edit column.
EditItemTemplate – It consists of two LinkButton for update and cancel column.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowEditing="OnRowEditing">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" />
<asp:LinkButton Text="Cancel" runat="server" OnClick="OnCancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Populating the GridView with a dynamic temporary DataTable
Inside the
Page_Load event handler, an object of
DataTable is created.
Then, three columns are added to the
DataTable Columns collection using the
AddRange method.
An Array of objects of type DataColumn is specified which will hold the name and the optional parameter Data Type i.e. the Type of the column.
Once the schema is ready i.e. all the columns are defined, some rows have been added using the Rows.Add method.
Finally, the dynamic temporary
DataTable is saved in a
ViewState variable and finally it is used to populate the
GridView control.
Note: You can also make use of
Session variable if you need to use the temporary
DataTable across the whole application.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
ViewState["dt"] = dt;
this.BindGrid();
}
}
protected void BindGrid()
{
gvCustomers.DataSource = ViewState["dt"]as DataTable;
gvCustomers.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(2) {
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
ViewState("dt") = dt
Me.BindGrid()
End If
End Sub
Protected Sub BindGrid()
gvCustomers.DataSource = TryCast(ViewState("dt"), DataTable)
gvCustomers.DataBind()
End Sub
Edit
When the
Edit Button is clicked, the GridView’s
OnRowEditing event handler is triggered, where the
EditIndex of the
GridView is updated with the
Row Index of the
GridView Row to be edited.
C#
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
gvCustomers.EditIndex = e.NewEditIndex;
this.BindGrid();
}
VB.Net
Protected Sub OnRowEditing(sender As Object, e As GridViewEditEventArgs)
gvCustomers.EditIndex = e.NewEditIndex
Me.BindGrid()
End Sub
GridView Row Update and Cancel Edit Events
When the Update Button is clicked, the GridView’s OnUpdate event handler is triggered.
Inside the
OnUpdate event handler, the temporary
DataTable is fetched back from the
ViewState variable and then the row to be updated is identified using the
RowIndex of the
GridView Row whose
Update button was clicked.
Then the values to be updated are fetched from the
TextBoxes and are assigned to the respective columns of the
DataTable Row.
Finally, the temporary
DataTable is saved back in ViewState and the
GridView is again populated with the updated temporary
DataTable.
C#
protected void OnUpdate(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string name = (row.Cells[0].Controls[0]as TextBox).Text;
string country = (row.Cells[1].Controls[0]as TextBox).Text;
DataTable dt = ViewState["dt"]as DataTable;
dt.Rows[row.RowIndex]["Name"] = name;
dt.Rows[row.RowIndex]["Country"] = country;
ViewState["dt"] = dt;
gvCustomers.EditIndex = -1;
this.BindGrid();
}
protected void OnCancel(object sender, EventArgs e)
{
gvCustomers.EditIndex = -1;
this.BindGrid();
}
VB.Net
Protected Sub OnUpdate(sender As Object, e As EventArgs)
Dim row As GridViewRow = TryCast(TryCast(sender,LinkButton).NamingContainer, GridViewRow)
Dim name As String = TryCast(row.Cells(0).Controls(0), TextBox).Text
Dim country As String = TryCast(row.Cells(1).Controls(0), TextBox).Text
Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
dt.Rows(row.RowIndex)("Name") = name
dt.Rows(row.RowIndex)("Country") = country
ViewState("dt") = dt
gvCustomers.EditIndex = -1
Me.BindGrid()
End Sub
Protected Sub OnCancel(sender As Object, e As EventArgs)
gvCustomers.EditIndex = -1
Me.BindGrid()
End Sub
Demo
Downloads