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.

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

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
            selectedRowsDict.Remove(DataGridView1.CurrentRow.Index)
        Else
            '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
            Else
                DataGridView1.Rows(rowNum).Selected = False
            End If
        Next
    End Sub

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.