|
-
May 23rd, 2009, 02:03 PM
#1
Thread Starter
Hyperactive Member
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
-
May 23rd, 2009, 04:44 PM
#2
Re: DataGridView Row Select
try this:
vb Code:
Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True
End If
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 23rd, 2009, 07:53 PM
#3
Re: DataGridView Row Select
I think he wants the whole row selected, not just the cell:
vb.net Code:
DataGridView1.Rows(e.RowIndex).Selected = True
-
May 23rd, 2009, 07:56 PM
#4
Re: DataGridView Row Select
another alternative:
vb Code:
DataGridView1.currentcell = DataGridView1(e.ColumnIndex,e.RowIndex)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 24th, 2009, 02:19 AM
#5
Thread Starter
Hyperactive Member
Re: DataGridView Row Select
Thanks Guys!
is using a while loop like this a bad idea, is their an easier/better way?
vb Code:
Private Sub Content_CellContentClick_1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Content.CellMouseDown Dim i As Integer = 0 While i < Content.Rows.Count Content.Rows(i).Selected = False i = i + 1 End While If e.Button = Windows.Forms.MouseButtons.Right Then Content.Rows(e.RowIndex).Selected = True End If End Sub
I am still very new to VB.NET, so I have MANY questions
-
May 24th, 2009, 02:33 AM
#6
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:
While Content.SelectedRows.Count > 0 Content.SelectedRows(0).Selected = False 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.
-
May 24th, 2009, 09:36 AM
#7
Re: DataGridView Row Select
 Originally Posted by The Little Guy
Thanks Guys!
is using a while loop like this a bad idea, is their an easier/better way?
vb Code:
Private Sub Content_CellContentClick_1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Content.CellMouseDown
Dim i As Integer = 0
While i < Content.Rows.Count
Content.Rows(i).Selected = False
i = i + 1
End While
If e.Button = Windows.Forms.MouseButtons.Right Then
Content.Rows(e.RowIndex).Selected = True
End If
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|