How to Play with DataGrid Control - Handling Item Edit Events
(Page 5 of 7 )
The EditCommand event is raised when the user clicks the Edit link in any row within the grid. For this event, we specified our dgNonApproveList_EditCommand event handler routine. Then we set the EditItemIndex property of the DataGrid control to the index of the row that contained the edit link the user clicked:
void dgNonApproveList_EditCommand(Object s, DataGridCommandEventArgs e)
{
dgNonApproveList.EditItemIndex=e.Item.ItemIndex;
BindGrid();
}
Handling the Update and Cancel Events
Now that we have the grid in “edit mode”, we just need to handle the update and cancel events. We specified that a click on the cancel link should execute our event handler named dgNonApproveList_CancleCommand. In this event handler, all we need to do is switch the grid back out of “edit mode” by existing the EditItemIndex property back to –1.
void dgNonApproveList_CancelCommand(Object s, DataGridCommandEventArgs e)
{
dgNonApproveList.EditItemIndex=-1;
BindGrid();
}
However, if the user clicks the update link, our dgNonApproveList_UpdateCommand will be called. Here we have to create a suitable SQL UPDATE statement in our example. For this we need to get the edited values from the DataGrid row that the user working on. Because we are updating dates, we are using Calendar. So it should not to go for validation test. In the case of textbox, we need to have validation. After declaring two variables to hold references to the calendar that contain the edited values, we first access the calDateFrom calendar using the FindControl method of the item that is contained in DataGridCommandEventArgs object passes to our event handler as a parameter. Notice that we have to convert the return value to the correct type like Calendar. It goes similar for second control that is calToDate.
Now that we have the reference of two Calendar control, we can create an SQL UPDATE statement and call our Command Object and its ExecuteNonQuery.
void dgNonApproveList_UpdateCommand(Object s, DataGridCommandEventArgs e)
{
string DocTId = dgNonApproveList.DataKeys[e.Item.ItemIndex].ToString();
string strConn=System.Configuration.ConfigurationSettings.AppSettings["strConn"];
Calendar calFrom;
Calendar calTo;
calFrom = (Calendar)e.Item.FindControl("calDateFrom");
calTo = (Calendar)e.Item.FindControl("calDateTo");
DateTime dtFrom = calFrom.SelectedDate.Date;
DateTime dtTo = calTo.SelectedDate.Date;
if (dtFrom<dtTo)
{
SqlConnection sqlConn = new SqlConnection(strConn);
sqlConn.Open();
SqlCommand objCommand = new SqlCommand();
objCommand.Connection=sqlConn;
SqlTransaction myTrans = sqlConn.BeginTransaction();
objCommand.Transaction=myTrans;
try
{
objCommand.CommandText="Update UserDocDefault Set DateTo='"+ dtFrom +"', DateTill='"+ dtTo +"' Where DocTId = "+ DocTId +"";
objCommand.ExecuteNonQuery();
myTrans.Commit();
}
catch (SqlException SqlEx)
{
MessageLabel.Text = SqlEx.Message;
myTrans.Rollback();
}
catch (Exception Ex)
{
MessageLabel.Text = Ex.Message;
}
finally
{
sqlConn.Close();
}
dgNonApproveList.EditItemIndex=-1;
BindGrid();
}
else
MessageLabel.Text = "Date From must be lower than Date Till";
}
We finish off by the grid back out of “edit mode” by setting the EditItemIndex property of the DataGrid control back to -1, and rebind the control to display the result.
Next: Handling Multiple Row Selection for Deletion by Checkbox Control >>
More ASP.NET Articles
More By Mayank Gupta