title: How to delete multiple rows in datagridview and database using vb.net window forms
body: <p>I have a data gridview which loads with these codes:</p>
  
  <pre><code>    mycom.Connection = cn
      mycom.CommandText = &lt;SQL&gt; SELECT UserName,Activity,CDate as `Date` FROM tbl_activity &lt;/SQL&gt;.Value
  
      Dim myadap As New MySqlDataAdapter(mycom)
      Dim mydt As New DataTable
  
      grdActivity.Columns.Add(colCB)
      myadap.Fill(mydt)
      grdActivity.DataSource = mydt
      myadap.Dispose()
  </code></pre>
  
  <p>And i have my delete button with these codes:</p>
  
  <pre><code>Dim selected As Integer
      selected = grdActivity.SelectedRows.Count
      If grdActivity.SelectedRows.Count &gt; 0 Then
          'you may want to add a confirmation message, and if the user confirms delete
          For a As Integer = 0 To selected
              myr.Close()
              mycom.Connection = cn
              mycom.CommandText = "Delete from tbl_activity where ID = '" &amp; a &amp; "' "
              mycom.ExecuteReader()
              myr.Close()
          Next
      Else
          MessageBox.Show("Select 1 row before you hit Delete")
      End If
  </code></pre>
  
  <p>Can anyone help me with the right codes so i can delete databases bounded rows not only in my datagridview but also directly from the database.. thanks..</p>