Hi!

I have following problem.
I need to select multiple random rows in DatagridView with keypress.
Ex:

Row1 -selected
Row2 -not selected
Row3 - selected

I do have part of code that will update certain field in database on keypress, but i have no idea how to create multiple selection and display it on DataGrid.

CTRL+Click is nice, but I would like to have option of selecting rows with keypress.

In following code I am checking if certain field is marked for selection(i want to give user an option to start selection, exit program then continue selection later) but it allways select all rows in table (even if only one is marked).

Dim NumRow As Integer

        NumRow = 0

        Do Until NumRow = dbtShow.RowCount - 1

            If dbtShow.Rows(dbtShow.CurrentRow.Index).Cells("Selected").Value.ToString() = "1" Then

                dbtShow.Rows(NumRow).Selected = True
                
            End If

            NumRow = NumRow + 1
        Loop

My question is: What is the best way to select multiple random rows in code?

Thank you in advance.

Dani AI

Generated

A few clarifications and a concise fix that tie back to ’s code and ’s suggestion:

The DataGridView must allow multiple selections (set MultiSelect = True) and use SelectionMode = FullRowSelect — do that once (for example in Form_Load). The main bug in ’s loop is that it repeatedly checks CurrentRow.Index instead of the loop index, and properties are being changed inside the loop; that will produce wrong results or clear selections. ’s snippet set MultiSelect = False, which prevents random multi-selection.

Restore visual selection from a saved "Selected" cell (do this after the grid is bound):

' Run once after data binding
dbtShow.MultiSelect = True
dbtShow.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dbtShow.ClearSelection()

For i As Integer = 0 To dbtShow.Rows.Count - 1
    If Not dbtShow.Rows(i).IsNewRow Then
        Dim v = dbtShow.Rows(i).Cells("Selected").Value
        If v IsNot Nothing AndAlso v.ToString() = "1" Then
            dbtShow.Rows(i).Selected = True
        Else
            dbtShow.Rows(i).Selected = False
        End If
    End If
Next

To toggle a row on a keypress (Space example) without clearing other selections and persist the flag back to the data source:

Private Sub dbtShow_KeyDown(sender As Object, e As KeyEventArgs) Handles dbtShow.KeyDown
    If e.KeyCode = Keys.Space Then
        Dim ri = If(dbtShow.CurrentCell, Nothing)?.RowIndex
        If ri IsNot Nothing AndAlso ri >= 0 Then
            dbtShow.Rows(ri).Selected = Not dbtShow.Rows(ri).Selected
            Dim cell = dbtShow.Rows(ri).Cells("Selected")
            cell.Value = If(cell.Value IsNot Nothing AndAlso cell.Value.ToString() = "1", "0", "1")
            ' If bound: BindingSource.EndEdit() and update the adapter to persist to DB
        End If
        e.Handled = True
    End If
End Sub

Troubleshooting: when bound to a DataTable check for DBNull before comparing, skip the new row (IsNewRow), call BindingSource.EndEdit() and adapter Update() to persist, and avoid changing selection properties inside tight loops.

Recommended Answers

All 3 Replies

Try this,

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dataGridView1.MultiSelect = False

Thank you for your response but this is not working for me.
English is not my native language so I am not sure if I am clear about my problem.

I would like to get same effect as CTRL+Click on multiple random rows but with keypress. Of course, since I am still learning VB.NET there is huge chance that i am doing everything wrong :)

Dim NumRow As Integer

        NumRow = 0

        Do Until NumRow = dbtShow.RowCount - 1

            If dbtShow.Rows(dbtShow.CurrentRow.Index).Cells("Selected").Value.ToString() = "1" Then

                dbtShow.Rows(NumRow).Selected = True
                dbtShowi.SelectionMode = DataGridViewSelectionMode.FullRowSelect
                dbtShow.MultiSelect = False
            End If

            NumRow = NumRow + 1
        Loop

This way one row is marked, and then selection jumps to last row, and clear that selection.

Thank you.

I specified above statement through (Grid) properties and when i try to select ctrl + mouseclick s working fine for random selection, shift + kepress also. ctrl+keypress it wont work for mulitple selections.

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.