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.

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.