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.
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()
).
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.
foreach(DataGridItem item in DataGrid1.Items)
{
props = new Props();
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;
}
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.
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()
).
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()
).
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.