Multiple Deletes in Gridview

Discussion in 'ASP .Net' started by MasterChief, Feb 7, 2006.

  1. MasterChief

    MasterChief Guest

    I have a customer that likes the gridview but instead of using the
    standard Delete command for each row he wants to be able to put a
    checkbox next to the row so he can chose multiple records and then
    chose a delete button up top. What is the best way to accomplish that.
    I have made a template in the gridview and added a checkbox. Then I
    made a command button and changed is CommandName to Delete but after
    that I don't really know what to do next.
     
    MasterChief, Feb 7, 2006
    #1
    1. Advertising

  2. You can achieve that by using a TemplateField with an ItemTemplate containing
    a CheckBox server control to build the first column of the GridView as I did
    in this demo:
    http://www.webswapp.com/codesamples/aspnet20/gridview_multiplerows_selection/

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "MasterChief" wrote:

    > I have a customer that likes the gridview but instead of using the
    > standard Delete command for each row he wants to be able to put a
    > checkbox next to the row so he can chose multiple records and then
    > chose a delete button up top. What is the best way to accomplish that.
    > I have made a template in the gridview and added a checkbox. Then I
    > made a command button and changed is CommandName to Delete but after
    > that I don't really know what to do next.
    >
    >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Feb 7, 2006
    #2
    1. Advertising

  3. MasterChief,

    Inside of the delete handler loop through the rows of the grid and look for
    each checkbox. Then if the box is checked run a delete command based on that
    row's id. Make certain that you set the GridView's DataKeyNames property to
    the primary key field of your database so you can retrieve the row's id:

    <asp:GridView ID="GridView1" runat="server" DataKeyNames="pk_InventoryId">


    Dim CheckBox As CheckBox
    Dim KeyId As Int32

    For Each GridViewRow As GridViewRow In GridView1.Rows
    '---Look at each checkbox here
    CheckBox = CType(GridViewRow.FindControl("MyCheckBox"), CheckBox)

    If CheckBox.Selected
    '---Get the database id
    KeyId = CType(GridView1.DataKeys.Item(GridViewRow.RowIndex).Value,
    Int32)

    '---Use the KeytId to delete from the database.
    End If
    Next

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "MasterChief" <> wrote in message
    news:...
    >I have a customer that likes the gridview but instead of using the
    > standard Delete command for each row he wants to be able to put a
    > checkbox next to the row so he can chose multiple records and then
    > chose a delete button up top. What is the best way to accomplish that.
    > I have made a template in the gridview and added a checkbox. Then I
    > made a command button and changed is CommandName to Delete but after
    > that I don't really know what to do next.
    >
     
    S. Justin Gengo [MCP], Feb 7, 2006
    #3
  4. Actually in cases like that the simplest way is to fall back to regular ASP
    style.
    Meaning just add checkbox (not a server control) to the column and give them
    name like
    "chkDelete" + RecordId ( concatenate with a recordId)

    then in you handler for the button you can have following code

    foreach( string sKey in Request.Form )
    {
    if( sKey.IndexOf("chkDelete") == 1 )
    {
    ......
    }
    }


    George.

    "MasterChief" <> wrote in message
    news:...
    >I have a customer that likes the gridview but instead of using the
    > standard Delete command for each row he wants to be able to put a
    > checkbox next to the row so he can chose multiple records and then
    > chose a delete button up top. What is the best way to accomplish that.
    > I have made a template in the gridview and added a checkbox. Then I
    > made a command button and changed is CommandName to Delete but after
    > that I don't really know what to do next.
    >
     
    George Ter-Saakov, Feb 7, 2006
    #4
  5. MasterChief

    MasterChief Guest

    I see how that works but my next question is about the delete code.
    What is the best way to connect to the database and do a delete in the
    code behind file?
     
    MasterChief, Feb 7, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Collens

    browser hangs when user deletes cookies

    Scott Collens, Mar 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    339
    bruce barker
    Mar 4, 2004
  2. =?Utf-8?B?TXBob3ph?=

    Confirming Deletes in a DataGrid

    =?Utf-8?B?TXBob3ph?=, Aug 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    448
    =?Utf-8?B?UmF2aWthbnRoW01WUF0=?=
    Aug 6, 2004
  3. John Dalberg
    Replies:
    1
    Views:
    4,237
    =?Utf-8?B?QnJhZCBSb2JlcnRz?=
    Aug 2, 2005
  4. Drew
    Replies:
    1
    Views:
    661
  5. dev648237923

    Alert to confirm deletes in the new GridView control

    dev648237923, Nov 17, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    716
    Steven Cheng[MSFT]
    Nov 20, 2006
Loading...

Share This Page