Click here to Skip to main content
15,358,603 members
Articles / Web Development / ASP.NET
Posted 6 Jan 2003


46 bookmarked

Caching in DataGrid while performing Insert, Edit and Update operations

Rate me:
Please Sign up or sign in to vote.
2.86/5 (21 votes)
6 Jan 20032 min read
This article shows how we can use caching while performing insert, edit and update functions in the DataGrid.

Sample Image - dgcache.gif


This simple demo represents how we can use caching while performing insert, edit and update functions in the DataGrid.


DataGrid can also display any objects that implement the IList interface. Here, for caching functionality, I am making use of ArrayList. Generally, if you want to perform an insert operation into the grid, you may have a form and accept all the values and enter it into the database and do a select operation to get the rows and then bind it to the DataGrid. Now, if you have EditCommandColumn then you may have to do the same update, select operation for every modification. In order to minimize the database calls and perform all the insert, edit and update, we can make use of a temporary cache such as ArrayList.

The ArrayList contains an object of class called Props.cs,that is my Property class. All it has is four properties: ID, First Name, Last Name and City.

    protected string m_strid;
    protected string m_strfname;
    protected string m_strlname;
    protected string m_strcity;

    public string id 
        return m_strid;

When a user clicks on 'InsertData', I get the input values, create an object of Props.cs class and set the values to the respective property. Then, I add this object to the ArrayList and call a function to bind the data (NewBindData()).

props = new Props(); = strid.Value;
props.fname = strfname.Value;
props.lname = strlname.Value; = strcity.Value;



In the NewBindData() method, I read all the items in my DataGrid. For each item (a row in DataGrid) in the DataGrid, I create an instance of the Props.cs class and set the corresponding property with the values of the respective cell. After filling the properties in Props.cs class for each item, I add it to the array list.

foreach(DataGridItem item in DataGrid1.Items) 
    props = new Props();

    //to read the ItemTemplate
    { = ((Label)item.Cells[1].FindControl("Label1")).Text;
    props.fname = ((Label)item.Cells[2].FindControl("Label2")).Text;
    props.lname = ((Label)item.Cells[3].FindControl("Label3")).Text; = ((Label)item.Cells[4].FindControl("Label4")).Text;
    //to read the EditItemTemplate
    else if(item.ItemType.ToString().Equals("EditItem")) 
    { = ((TextBox)item.Cells[1].FindControl("Textbox1")).Text;
    props.fname = ((TextBox)item.Cells[2].FindControl("Textbox2")).Text;
    props.lname = ((TextBox)item.Cells[3].FindControl("Textbox3")).Text; = ((TextBox)item.Cells[4].FindControl("Textbox4")).Text;


Once all the items in the DataGrid are read and filled in the ArrayList as objects of properties, I set the DataGrid's DataSource to this ArrayList and bind the data.

DataGrid1.DataSource = list;

So, for every insertion, this process will be repeated. In my DataGrid, I use EditCommandColumn to perform the edit and update functions. Whenever the edit column is clicked, it refers to OnEdit() function in the codebehind page and all I do is set the edit item index and call the function to bind data (NewBindData()).

protected void OnEdit(object source,
      System.Web.UI.WebControls.DataGridCommandEventArgs e)
DataGrid1.EditItemIndex = e.Item.DataSetIndex;

Similarly, for update and cancel, I set the edit item index to default value (-1) and call the function to bind data (NewBindData()).

DataGrid1.EditItemIndex = -1;


For all the insert, edit and update operations, we are not making use of any database operations at all. You can have a submit button in your page at the end that says "Submit all changes". Once this is clicked then you can do the one time database operation.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
No Biography provided

Comments and Discussions

QuestionDatalist need to display Hyperlink? Pin
R. M. Sohail Arshed5-Sep-07 0:23
MemberR. M. Sohail Arshed5-Sep-07 0:23 
QuestionHow to Synchronous the data if multi-user access the database in the same time? Pin
kosmas15-Sep-05 14:50
Memberkosmas15-Sep-05 14:50 
GeneralWhy not just.. Pin
speedi1239-Aug-04 10:38
Memberspeedi1239-Aug-04 10:38 
GeneralRe: Why not just.. Pin
extremeg13-Jul-06 3:37
Memberextremeg13-Jul-06 3:37 
I once implemented this type of data caching to avoid multiple database calls for every CUD (Create, Delete, Operation), but obviously the robust approach is to:
speedi123 wrote:
Store the DataSet in the Session or ViewState on your data bind operation and then retreive it and use it repeatably?

DataAdapter.Update() method does all the trick in one line. Smile | :)

...the mind is not a vessel to be filled but a fire to ignited
GeneralHand cursor on datagrid columnheader or caption area Pin
Neeraj Jain18-Nov-03 12:16
MemberNeeraj Jain18-Nov-03 12:16 
GeneralUnless I'm missing something here... Pin
Marc Clifton7-Jan-03 11:51
mvaMarc Clifton7-Jan-03 11:51 
GeneralRe: Unless I'm missing something here... Pin
anandaji8-Jan-03 2:34
Memberanandaji8-Jan-03 2:34 

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.