I have a datagridview with 10 rows I can select mutiple rows by using Ctrl & Mouse click
but what I would like to do is use a button to select the row.

I.E the user could select row one, click the button and row one would be selected
then select row 3 and press the button. This would then select this row as well as row one

and so on and so on.

4 Years
Discussion Span
Last Post by Mike Bishop

How are you identifying which row the user wants to select? ComboBox? TextBox? Mind reading?

Edited by cgeier


i will try the mind reading way, but not sure how to code it in .net..

the user will select the row using the mouse. then use the button.

what i am trying to do it select a few rows with out the need to use Ctrl & mouse click.

not even sure if this is possible. sorry its difficult to explain
does this help


Something like the following would work:

    'keeps track of selected rows
    Private selectedRowsDict As New Dictionary(Of Integer, String)

DataGridView MouseClick event:

    Private Sub DataGridView1_MouseClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
        'allows for row de-selection by toggling
        'between row being selected and de-selected
        If selectedRowsDict.ContainsKey(DataGridView1.CurrentRow.Index) Then
            'deselect row
            'select row
            selectedRowsDict.Add(DataGridView1.CurrentRow.Index, "Selected")
        End If

        'loop through all rows in DataGridView
        'de-select rows that have been de-selected
        'select rows that have been selected
        For rowNum As Integer = 0 To DataGridView1.RowCount - 1
            If selectedRowsDict.ContainsKey(rowNum) Then
                DataGridView1.Rows(rowNum).Selected = True
                DataGridView1.Rows(rowNum).Selected = False
            End If
    End Sub

Edited by cgeier


cgeier thats exactly what i was looking for, thank you so much.

PS do you have any code for the mind reading scenario?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.