Click here to Skip to main content
11,924,825 members (45,612 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


17 bookmarked

Temporary Values in Grid

, 5 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Temporary Values in Grid


This article is developed in C#. It is about saving temporary values in Grid and adding rows one by one and maintaining the values.

Adding Three Empty Rows in a Grid

The below given code helps to add 3 empty rows inside a grid with four columns:


Saving the Values in a Temporary DataTable in order to Maintain the Values

The below code will save the records given in the grid and also maintain the values in a View state.

grid2.jpg - Click to enlarge image

Maintaining Values of the DataTable in ViewState

The below code will maintain the values in the View state:


Copy the Code and Paste it in .ASPX

<%@ Page Language="C#" AutoEventWireup="true" 
	CodeFile="TempTable.aspx.cs" Inherits="TempTable" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="" >
<head runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<asp:Button ID="btnNew" runat="server" OnClick="btnNew_Click" Text="New" /><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
	DataKeyNames="sino" CellPadding="4" ForeColor="#333333" GridLines="None">
<asp:TemplateField HeaderText="Name">
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>' ></asp:TextBox>
<asp:TemplateField HeaderText="Gender">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" 
	RepeatDirection="Horizontal" Text='<%# Bind("gender") %>' >

Copy the Code and Paste it in .ASPX.CS

using System;
 using System.Data;
 using System.Configuration;
 using System.Collections;
 using System.Web;
 using System.Web.Security;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Web.UI.HtmlControls;
public partial class TempTable : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)

//To Bind Empty Grid with three rows when page loads

public void EmptyGrid()
DataTable TempTable = new DataTable();
TempTable.PrimaryKey = new DataColumn[] { TempTable.Columns["sino"] };
TempTable = BindEmptyRows(3, TempTable);
GridView1.DataSource = TempTable;

//Method to Bind Rows

private DataTable BindEmptyRows(int nofoRows, DataTable dt)
for (int i = 0; i < nofoRows; i++)
DataRow dr = dt.NewRow();
dr["sino"] = dt.Rows.Count + 1;
dr["name"] = string.Empty;
dr["gender"] = "0";
dr["qualification"] = "0";
return dt;

//On Clicking Save Button this event fires

protected void btnSave_Click(object sender, EventArgs e)

//Save Grid in Temporary Datatable

private void SaveGridDataInTempTable()
DataTable dtTemp = new DataTable(); 
if (dtTemp == null || dtTemp.Columns.Count <= 0)
dtTemp.Columns.Add("name", typeof(String));
dtTemp.Columns.Add("gender", typeof(String));
dtTemp.Columns.Add("qualification", typeof(String));
dtTemp.PrimaryKey = new DataColumn[] { dtTemp.Columns["sino"] };
foreach (GridViewRow gvRow in GridView1.Rows)
DataRow drTemp = dtTemp.NewRow();
drTemp["sino"] = int.Parse(GridView1.DataKeys[gvRow.RowIndex]["sino"].ToString());
drTemp["name"] = ((TextBox)gvRow.FindControl("TextBox1")).Text.Trim();
drTemp["gender"] = 
drTemp["qualification"] = 
Temp = dtTemp;

//On Clicking New Button this event fires

protected void btnNew_Click(object sender, EventArgs e)
DataTable dtNew = Temp.Copy();
bool isinsert = true;
foreach (DataRow dr in dtNew.Rows)
if (String.IsNullOrEmpty(dr["name"].ToString()) || 
|| dr["qualification"].ToString() == "0")
isinsert = false;
if (isinsert)
BindEmptyRows(1, dtNew);
Temp = dtNew;
GridView1.DataSource = dtNew;

//Property to maintain the Datatable

public DataTable Temp
object o = ViewState["Temp"];
if (o == null)
DataTable dt = new DataTable();
return dt;
return (DataTable)o;
ViewState["Temp"] = value;

Save Button in the Interface

The save button in the interface will save the values in a Temporary datatable and maintain it in a View State. We can also modify the values and save them in a temporary Datatable.

NewButton in the Interface

The new button in the interface will add a new row by checking whether the previous rows are not empty. If the previous rows are empty, then it won't add a new row. If the values are entered in the previous rows, then a new row will be entered.


This is my first article on The Code Project. I hope this will be useful to you. If you have any suggestions or find any mistakes, please leave a comment below.


  • 5th May, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

padmanabhan N
Software Developer
India India
Completed my MCA in Madras University.
MCTS in Microsoft® .NET Framework 2.0 - Web-based Client
MCTS in Microsoft® .NET Framework 2.0 - Application Development

You may also be interested in...

Comments and Discussions

QuestionRe : Allow Page Pin
Member 75098523-Jul-11 5:16
memberMember 75098523-Jul-11 5:16 
AnswerRe: Re : Allow Page Pin
padmanabhan N23-Jul-11 5:23
memberpadmanabhan N23-Jul-11 5:23 
GeneralMy vote of 3 Pin
just p15-Aug-10 6:14
memberjust p15-Aug-10 6:14 
i think this is written too good and easy way.......
GeneralVery Useful Pin
thatraja19-Jan-10 3:36
memberthatraja19-Jan-10 3:36 
GeneralRe: Very Useful Pin
padmanabhan N19-Jan-10 4:22
memberpadmanabhan N19-Jan-10 4:22 
GeneralNICI Pin
jdhforever11-Nov-09 16:53
memberjdhforever11-Nov-09 16:53 
GeneralRe: NICI Pin
padmanabhan N15-Nov-09 20:00
memberpadmanabhan N15-Nov-09 20:00 
Generalnice article Pin
Uma J6-May-09 7:21
memberUma J6-May-09 7:21 
GeneralRe: nice article Pin
padmanabhan N13-May-09 7:10
memberpadmanabhan N13-May-09 7:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151125.3 | Last Updated 5 May 2009
Article Copyright 2009 by padmanabhan N
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid