Multiple selection with mouse in Datagridview VB.NET


I have a Datagridview DGV_Historial_Ventas with the sales records and another DGV_Historial_Ventas_Articulos with the items of the sale selected in the first one (DGV_Historial_Ventas)

The DGV_Historial_Ventas has the property multirow , that is to say that I can choose several sales so that in the DGV_Historial_Ventas_Articulos all the articles that belong to those sales are displayed.

Everything works perfect, when I select several rows with CTRL or with SHIFT. But if I make a selection with the mouse (drag and drop) the event does not fire:

    Private Sub DGV_Ventas_Historial_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV_Ventas_Historial.CellClick

    If DGV_Ventas_Historial.RowCount > 0 And DGV_Ventas_Historial.SelectedRows.Count > 0 Then

        Label_Ventas_HistorialArticulos_Total.Text = "Total: " & DGV_Ventas_Historial.Item("TotalArticulos", e.RowIndex).Value.ToString & " Articulos"
        If DGV_Ventas_Historial.SelectedRows.Count = 0 Then DGV_Ventas_Historial_Articulos.DataSource = ""
    End If
End Sub

I thought about using the MouseUp event but it uses and the DataGridViewCellMouseEventArgs which has mouse coordinates and I do not know how to use it.

In CellClick use e as DataGridViewCellEventArgs whereby I can use e.rowindex to know the row that is being clicked (which is something I need)

asked by Gonzalo 23.05.2016 в 06:22

2 answers


The MouseUp event handler does not receive a DataGridViewCellMouseEventArgs object (which does have RowIndex and ColumnIndex properties), but a MouseEventArgs object (which does not have them).

I can think of two options:

  • Use the CellMouseUp event whose handler receives a DataGridViewCellMouseEventArgs object through which you can access the row and column in which the event was generated

  • Use the MouseUp event and obtain the information of the cell in which it is produced using the HitTest method of the DataGridView that gives you a HitTestInfo object with information about the row and column located at a certain position:

    Private Sub DataGridView1_MouseUp(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseUp
        Dim info As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y)
        Dim row = info.RowIndex
        Dim column = info.ColumnIndex
    End Sub
  • answered by 23.05.2016 / 09:31

    I use this method when I want to delete, I use it for up to 5 objects but a list could be used.

    I call the method click on the accept button

    Private Sub ElementosSeleccionados(aux As DataGridView)
        Dim id, i As Integer
        Dim idList(5)
        For Each selectedItem As DataGridViewRow In aux.SelectedRows
            'show ids of multiple selected rows
            id = selectedItem.Cells("ID").Value
            idList(i) = id
            i += 1
        Next selectedItem
        Dim sResult As String = ""
        For Each elem As String In idList
            sResult &= elem & ", "
        MsgBox("Seleccionados" & sResult)
    End Sub
    answered by 05.12.2018 в 17:54