Wednesday, February 21, 2007

GridViews with DataTables - Part II

Part II:

Now to handle Edit,update,cancel and delete operations in the Gridview..we will do the following....

Edit The row: To do an edit on the row, we need to write the event on RowEditing. If you dont know where to find this, click on the GridView, click properties.on the properties tab you will see a small Flashing symbol(On the top).Double click on the event called RowEditing and it will open the code below.

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; //This will enable the row clicked in the Grid into an editable mode.
if (Session["tables"] != null)
{
GridView1.DataSource = Session["tables"];
GridView1.DataBind();
}
}

What we do above is, we set the EditIndex to be the currently clicked row in the Grid and bind the Grid again with our Datasource.

Cancel the Edit Option: The most easiest of all, Write the event in RowCanceling.

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; //Restore to normal.
if (Session["rows"] != null)
{
myDatatable = (DataTable)Session["rows"];
GridView1.DataSource = myDatatable;
GridView1.DataBind();
}
}

Update a Grid to a datatable: The event associated to do this RowUpdating. what we do here is, we find the row that was clicked to do an update.then, find the textbox value and bind it back to the grid. and we also set the EditIndex to -1 so that the Grid looks normal. I had difficulty in finding how to identify the textbox since i didnt create any template column


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex]; // This statement finds the row that was clicked.
GridView1.EditIndex = -1; // Change the Grid to Normal view.

if (row != null)
{
TextBox t = (TextBox)row.Cells[1].Controls[0]; //This will find the control in Cells[1] in the grid and since we know it is a textbox //we are casting it to a temporary textbox t

myDatatable = (DataTable)Session["tables"]; //Get the old datatable from the session variable

//Till the end of the datatable, we traverse till we find the DataTable's row and the row in the grid are equal..and update the datatable with //the new value.
for (int i = 0; i less than myDatatable.Rows.Count; i++)
{
if (e.RowIndex == i)
{

myDatatable.Rows[i][0] = Convert.ToInt32 (t.Text); //Assign the textbox value to the current row.

Session["tables"] = myDatatable; //Set the new Datatable to the session

GridView1.DataSource = myDatatable;
GridView1.DataBind ();
}
}
}
}

Delete the Row : We are almost done.! just one more step.follow the comments next to the statements, straightforward stuff..

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex]; // Find which row was clicked to delete?
if (row != null)
{
myDatatable = (DataTable)Session["rows"];
for (int i = 0; i less than myDatatable.Rows.Count ; i++)
{
if (e.RowIndex == i)
myDatatable.Rows.RemoveAt(i); //Remove that row.
Session["tables"] = myDatatable; //assign the new datatable to the session.

//Bind to the Grid again.! Everyone is happy !
GridView1.DataSource = Session["tables"];
GridView1.DataBind();
}
}
}
}

So we have done all the basic binding,update,edit,cancel,delete on a grid from a datatable. if you have any questions feel free to contact me on this particular topic.

Till then, Happy Coding :).

No comments: