asp.net.ph

DataGrid.DeleteCommand Event

System.Web.UI.WebControls Namespace   DataGrid Class


Occurs when a delete command button associated with an item in the DataGrid control is clicked.

[ VB ]
Public Event DeleteCommand As DataGridCommandEventHandler

[ C# ]
public event DataGridCommandEventHandler DeleteCommand;

[ C++ ]
public: __event DataGridCommandEventHandler* DeleteCommand;

In [ JScript ], you can handle the events defined by a class, but you cannot define your own.

Remarks

The DeleteCommand event is raised whenever a Delete button associated with an item in the DataGrid control is clicked. The event is typically used to trigger a handler that removes the item in the DataGrid control.

Controls such as delete command buttons that are defined within the items of a parent container such as the DataGrid bubbles their event up the control hierarchy to the containing DataGrid control. This provides a convenient way to assign one handler for the same command event at the DataGrid level, as only one delete command can be activated at any time for any item in the grid. You do not have to assign the same handler for each delete button.

Event Data

Information related to the DeleteCommand event is passed via a DataGridCommandEventArgs object to the method assigned to handle the event. The following DataGridCommandEventArgs properties provide information specific to this event.

Property Description
CommandArgument ( inherited from CommandEventArgs ) Gets the argument for the command.
CommandName ( inherited from CommandEventArgs ) Gets the name of the command.
CommandSource Gets the source of the command.
Item Gets the selected item in the DataGrid control.

Example

The following example demonstrates how to code a handler for the DeleteCommand event to delete a single item from a DataGrid control.

void deleteItem ( Object src, DataGridCommandEventArgs e ) {
   // get key to find in data source
   string empToDelete = ( ( LinkButton ) e.Item.FindControl ( "itemToDelete" ) ).Text;

   // load data from Session
   DataTable myTable = ( DataTable ) Session [ "myTable" ];
   DataRowCollection myRows = myTable.Rows;

   // get row to delete
   DataRow thisRow = myRows.Find ( empToDelete );
   if ( thisRow != null ) myRows.Remove ( thisRow );

   // refresh data in Session
   Session [ "myTable" ] = myTable;
   // rebind grid to updated data in Session
   myGrid.DataSource = Session [ "myTable" ];
   myGrid.DataBind ( );
}
  C# VB

 Show me 

The following example demonstrates how to enable multiple item deletions in a DataGrid control.

void deleteItems ( Object src, EventArgs e ) {
   // load data from Session
   DataTable myTable = ( DataTable ) Session [ "myTable" ];
   // get rows collection
   DataRowCollection myRows = myTable.Rows;

   // initialize counter
   int deleted = 0;

   // loop thru each datagrid item and see if item should be deleted.
   foreach ( DataGridItem item in myGrid.Items ) {
      HtmlInputCheckBox cbItem = ( HtmlInputCheckBox ) item.FindControl ( "cbItem" );
      if ( cbItem.Checked ) {
         DataRow thisRow = myRows.Find ( cbItem.Value );
         if ( thisRow != null ) myRows.Remove ( thisRow );
         deleted ++;
      }
   }
   // refresh data in Session
   Session [ "myTable" ] = myTable;
   // rebind grid to updated data in Session
   myGrid.DataSource = Session [ "myTable" ];
   myGrid.DataBind ( );
   myGrid.Visible = ( myGrid.Items.Count > 0 );
}
  C# VB

 Show me 

See Also

DataGrid Members   CancelCommand   EditCommand   UpdateCommand   Allowing Users to Delete Items in a DataGrid Control Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph