Multiple Deletes in Gridview

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MasterChief

    Multiple Deletes in Gridview

    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.

  • Phillip Williams

    #2
    RE: Multiple Deletes in Gridview

    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:


    --
    HTH,
    Phillip Williams




    "MasterChie f" wrote:
    [color=blue]
    > 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.
    >
    >[/color]

    Comment

    • S. Justin Gengo [MCP]

      #3
      Re: Multiple Deletes in Gridview

      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="p k_InventoryId">


      Dim CheckBox As CheckBox
      Dim KeyId As Int32

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

      If CheckBox.Select ed
      '---Get the database id
      KeyId = CType(GridView1 .DataKeys.Item( GridViewRow.Row Index).Value,
      Int32)

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

      --
      Sincerely,

      S. Justin Gengo, MCP
      Web Developer / Programmer



      "Out of chaos comes order."
      Nietzsche
      "MasterChie f" <constants@mi x-net.net> wrote in message
      news:1139329779 .029369.175490@ g14g2000cwa.goo glegroups.com.. .[color=blue]
      >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.
      >[/color]


      Comment

      • George Ter-Saakov

        #4
        Re: Multiple Deletes in Gridview

        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("c hkDelete") == 1 )
        {
        ......
        }
        }


        George.

        "MasterChie f" <constants@mi x-net.net> wrote in message
        news:1139329779 .029369.175490@ g14g2000cwa.goo glegroups.com.. .[color=blue]
        >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.
        >[/color]


        Comment

        • MasterChief

          #5
          Re: Multiple Deletes in Gridview

          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?

          Comment

          Working...