Results 1 to 7 of 7

Thread: DataGridView Row Select

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    284

    DataGridView Row Select

    Is there a way to manually select a row?

    Right now, it uses the default selection method where when you left click it highlights the row, but I would like it so when your right click on a row it also selects the row, because I would like that row to be active when the row is right clicked, and not just left clicked.
    I am still very new to VB.NET, so I have MANY questions

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: DataGridView Row Select

    try this:

    vb Code:
    1. Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
    2.     If e.Button = Windows.Forms.MouseButtons.Right Then
    3.         DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True
    4.     End If
    5. End Sub

  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: DataGridView Row Select

    I think he wants the whole row selected, not just the cell:

    vb.net Code:
    1. DataGridView1.Rows(e.RowIndex).Selected = True

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: DataGridView Row Select

    another alternative:

    vb Code:
    1. DataGridView1.currentcell = DataGridView1(e.ColumnIndex,e.RowIndex)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    284

    Re: DataGridView Row Select

    Thanks Guys!

    is using a while loop like this a bad idea, is their an easier/better way?

    vb Code:
    1. Private Sub Content_CellContentClick_1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Content.CellMouseDown
    2.         Dim i As Integer = 0
    3.         While i < Content.Rows.Count
    4.             Content.Rows(i).Selected = False
    5.             i = i + 1
    6.         End While
    7.         If e.Button = Windows.Forms.MouseButtons.Right Then
    8.             Content.Rows(e.RowIndex).Selected = True
    9.         End If
    10.     End Sub
    I am still very new to VB.NET, so I have MANY questions

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: DataGridView Row Select

    Yep there is a better way, DataGridViews has a property called 'SelectedRows'. So instead of you looping through every row in the DataGridView you can just use that collection:

    vb.net Code:
    1. While Content.SelectedRows.Count > 0
    2.         Content.SelectedRows(0).Selected = False
    3. End While

    Both your code and mine does the same, but I'm just looping through the rows that are selected and making them not selected.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: DataGridView Row Select

    Quote Originally Posted by The Little Guy View Post
    Thanks Guys!

    is using a while loop like this a bad idea, is their an easier/better way?

    vb Code:
    1. Private Sub Content_CellContentClick_1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Content.CellMouseDown
    2.         Dim i As Integer = 0
    3.         While i < Content.Rows.Count
    4.             Content.Rows(i).Selected = False
    5.             i = i + 1
    6.         End While
    7.         If e.Button = Windows.Forms.MouseButtons.Right Then
    8.             Content.Rows(e.RowIndex).Selected = True
    9.         End If
    10.     End Sub

    or you could use my code from post #4, which will select the cell you click on + deselect the last cell, which with fullrowselect is what you want.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width