Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Datagrid: Delete multiple rows using multiple checkboxes and one Delete button


Message #1 by Romy Jarr <romyjarr@t...> on Tue, 29 Oct 2002 22:38:08 +0800
Hello guys,

How can I do deletion of multiple rows (with one checkbox for every rows) inside a datagrid by clicking one command button i.e.
Delete button? This 'Delete' button is placed outside the datagrid together with 'Select All' and 'Clear All'.  I know how to code
the function for both 'Select All' and 'Delete All', but not the 'Delete' one.  The user shall select/check any checkboxes for any
rows...once he/she click at the 'Delete' button, all the selected/checked records will be deleted from the database.  This
functionality works sort of like in Hotmail/Yahoo Mail fashion.  I am aware of http://www.datagridgirl.com/rowselector.aspx, I can't
seem to get this to work the way I want it.  Can anyone point me to this implementation?

The below code snippet is the method definition when the user want to delete a particular row by clicking at the delete icon for the
respective row.  This is set using the datagrid attribute namely OnDeleteCommand="NewsGrid_Delete":

protected void NewsGrid_Delete(object sender, DataGridCommandEventArgs e)
{
    UnselectGridItem( );
    // delete the news identified by the ID of this row
    NewsMailer.NewsHistoryDB news = new NewsMailer.NewsHistoryDB(GetConnString());
    news.Delete( (int)NewsGrid.DataKeys[e.Item.ItemIndex] );
    BindGrid( );
}

The DayaKeyField for the datagrid (i.e. NewsGrid) is the ID of each record.

Thanks in advance for the help.

Kindly regards,
Romy
Petronas Twin Tower (KLCC),
Kuala Lumpur,
Malaysia.

Message #2 by "Anthony Bouch" <tony@a...> on Wed, 30 Oct 2002 23:01:35 +0700
Romy - check out the sample application at www.IbuySpy.com from
Microsoft - you can download the source code for this site.

Here's a snippet from this application that removes all the checkbox
checked items (as well as those containing a zero in a text box)

 void UpdateShoppingCartDatabase() {

            IBuySpy.ShoppingCartDB cart = new IBuySpy.ShoppingCartDB();

            // Obtain current user's shopping cart ID
            String cartId = cart.GetShoppingCartId();

            // Iterate through all rows within shopping cart list
            for (int i=0; i < MyList.Items.Count; i++) {

                // Obtain references to row's controls
                TextBox quantityTxt = (TextBox)
MyList.Items[i].FindControl("Quantity");
                CheckBox remove = (CheckBox)
MyList.Items[i].FindControl("Remove");

                // Wrap in try/catch block to catch errors in the event
that someone types in
                // an invalid value for quantity
                int quantity;
                try {
                    quantity = Int32.Parse(quantityTxt.Text);

                    // If the quantity field is changed or delete is
checked
                    if (quantity != (int)MyList.DataKeys[i] ||
remove.Checked == true) {

                        Label lblProductID = (Label)
MyList.Items[i].FindControl("ProductID");

                        if (quantity == 0 || remove.Checked == true) {
                            cart.RemoveItem(cartId,
Int32.Parse(lblProductID.Text));
                        }
                        else {
                            cart.UpdateItem(cartId,
Int32.Parse(lblProductID.Text),quantity);
                        }
                    }
                }
                catch {
                    MyError.Text = "There has been a problem with one or
more of your inputs.";
                }
            }
        }


Subject: Datagrid: Delete multiple rows using multiple checkboxes and
one  Delete button
From: Romy Jarr <romyjarr@t...>
Date: Tue, 29 Oct 2002 22:38:08 +0800
X-Message-Number: 2

Hello guys,

How can I do deletion of multiple rows (with one checkbox for every
rows) inside a datagrid by clicking one command button i.e. Delete
button? This 'Delete' button is placed outside the datagrid together
with 'Select All' and 'Clear All'.  I know how to code the function for
both 'Select All' and 'Delete All', but not the 'Delete' one.  The user
shall select/check any checkboxes for any rows...once he/she click at
the 'Delete' button, all the selected/checked records will be deleted
from the database.  This functionality works sort of like in
Hotmail/Yahoo Mail fashion.  I am aware of
http://www.datagridgirl.com/rowselector.aspx, I can't seem to get this
to work the way I want it.  Can anyone point me to this implementation?

The below code snippet is the method definition when the user want to
delete a particular row by clicking at the delete icon for the
respective row.  This is set using the datagrid attribute namely
OnDeleteCommand="NewsGrid_Delete":

protected void NewsGrid_Delete(object sender, DataGridCommandEventArgs
e) {
    UnselectGridItem( );
    // delete the news identified by the ID of this row
    NewsMailer.NewsHistoryDB news = new
NewsMailer.NewsHistoryDB(GetConnString());
    news.Delete( (int)NewsGrid.DataKeys[e.Item.ItemIndex] );
    BindGrid( );
}

The DayaKeyField for the datagrid (i.e. NewsGrid) is the ID of each
record.

Thanks in advance for the help.

Kindly regards,
Romy
Petronas Twin Tower (KLCC),
Kuala Lumpur,
Malaysia.


  Return to Index