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.
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.
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. |
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 ( );
}
Sub deleteItem ( src As Object, e As DataGridCommandEventArgs )
' get key to find in data source
Dim empToDelete As String = CType ( e.Item.FindControl ( "itemToDelete" ), LinkButton ).Text
' load data from Session
Dim myTable As DataTable = CType ( Session ( "myTable" ), DataTable )
Dim myRows As DataRowCollection = myTable.Rows
' get row to delete
Dim thisRow As DataRow = myRows.Find ( empToDelete )
If Not ( thisRow Is Nothing ) Then myRows.Remove ( thisRow )
' refresh data in Session
Session ( "myTable" ) = myTable
' rebind grid to updated data in Session
myGrid.DataSource = Session ( "myTable" )
myGrid.DataBind ( )
End Sub |
|
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 );
}
Sub deleteItems ( src As Object, e As EventArgs )
' load data from Session
Dim myTable As DataTable = CType ( Session ( "myTable" ), DataTable )
' get rows collection
Dim myRows As DataRowCollection = myTable.Rows
' initialize counter
Dim deleted As Integer = 0
' loop thru each datagrid item and see if item should be deleted.
Dim item As DataGridItem
For Each item In myGrid.Items
Dim cbItem As HtmlInputCheckBox = CType ( item.FindControl ( "cbItem" ), HtmlInputCheckBox )
If cbItem.Checked Then
Dim thisRow As DataRow = myRows.Find ( cbItem.Value )
If Not ( thisRow Is Nothing ) Then myRows.Remove ( thisRow )
deleted += 1
End If
Next item
' 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
End Sub |
|
C# |
VB |
Show me
DataGrid Members CancelCommand EditCommand UpdateCommand Allowing Users to Delete Items in a DataGrid Control