Click here to Skip to main content
15,867,141 members
Articles / Web Development / ASP.NET
Article

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 140.4K   7.1K   46   7
This article shows how we can use caching while performing insert, edit and update functions in the DataGrid.

Sample Image - dgcache.gif

Introduction

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

Walk-through

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.

C#
    protected string m_strid;
    protected string m_strfname;
    protected string m_strlname;
    protected string m_strcity;

    public string id 
    {
    get
    {
        return m_strid;
    }
    set 
    {
            m_strid=value;
    }
}

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()).

C#
props = new Props();
props.id = strid.Value;
props.fname = strfname.Value;
props.lname = strlname.Value;
props.city = strcity.Value;

list.Add(props);

NewBindData();

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.

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

    //to read the ItemTemplate
    if(!item.ItemType.ToString().Equals("EditItem"))
    {
    props.id = ((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;
    props.city = ((Label)item.Cells[4].FindControl("Label4")).Text;
    }
    //to read the EditItemTemplate
    else if(item.ItemType.ToString().Equals("EditItem")) 
    {
    props.id = ((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;
    props.city = ((TextBox)item.Cells[4].FindControl("Textbox4")).Text;
    }

    list.Add(props);
}

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.

C#
DataGrid1.DataSource = list;
DataGrid1.DataBind();

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()).

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

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

C#
DataGrid1.EditItemIndex = -1;
NewBindData();

Conclusion

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.

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDatalist need to display Hyperlink? Pin
R. M. Sohail Arshed5-Sep-07 0:23
R. 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
kosmas15-Sep-05 14:50 
GeneralWhy not just.. Pin
speedi1239-Aug-04 10:38
speedi1239-Aug-04 10:38 
GeneralRe: Why not just.. Pin
extremeg13-Jul-06 3:37
extremeg13-Jul-06 3:37 
GeneralHand cursor on datagrid columnheader or caption area Pin
Neeraj Jain18-Nov-03 12:16
Neeraj Jain18-Nov-03 12:16 
Well, I know my this question is not relevant but I am dying to get the answer, please let me know if you know any way around this problem.

I have a System.Windows.Forms.Datagrid control and I want to change the mouse to hand when it comes over the caption area and column header. Any idea how can I do that. I search in Datagrid but no luck.

Waiting for ur reply...
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
anandaji8-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.