473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

DataGridView: Force row selection on right-click?

Anyone know how to force the row to select when user right clicks the grid?
I have SelectionMode set to DataGridViewSelectionMode.FullRowSelect.
I tried this...

Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Dim mouseArg As New
System.Windows.Forms.MouseEventArgs(Windows.Forms. MouseButtons.Left, 1, e.X,
e.Y, 0)
DataGridView1_MouseDown(sender, mouseArg)
End If
End Sub

-Spencer
Nov 23 '05 #1
2 18043
Here's the answer:

With the DataGridView in VS 2005 there are two ways to do this.

The first is to use the DataGridView.HitTest to get the row index. Then
select the row using that index

The second and easier option is to use the DataGridView_CellMouseClick
event. The event passes a
System.Windows.Forms.DataGridViewCellMouseEventArg s that has the row index
attached. Here's the code I used:

Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e
As _
System.Windows.Forms.DataGridViewCellMouseEventArg s)_
Handles DataGridView1.CellMouseClick

If e.Button = Windows.Forms.MouseButtons.Right Then
DataGridView1.Rows(e.RowIndex).Selected = True
End If
End Sub

Nov 23 '05 #2
I would change this:
If e.Button = Windows.Forms.MouseButtons.Right Then

to:
If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex >= 0 Then

or you'll get an error if right-clicking on the header.

Greg
"Spencer Williamson" <ve******@nospamhotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Here's the answer:

With the DataGridView in VS 2005 there are two ways to do this.

The first is to use the DataGridView.HitTest to get the row index. Then
select the row using that index

The second and easier option is to use the DataGridView_CellMouseClick
event. The event passes a
System.Windows.Forms.DataGridViewCellMouseEventArg s that has the row index
attached. Here's the code I used:

Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e
As _
System.Windows.Forms.DataGridViewCellMouseEventArg s)_
Handles DataGridView1.CellMouseClick

If e.Button = Windows.Forms.MouseButtons.Right Then
DataGridView1.Rows(e.RowIndex).Selected = True
End If
End Sub

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bob | last post by:
I have a combox column in a datagridview (vs2005 and VB.NET). Once I select an item from the dropdownlist in the combox and do a save, I can no longer remove the item. I can change my selection but...
4
by: steve | last post by:
Hi All I have a datagridview bound to a datatable Due to the use of touch screens I need to be able to scroll the grid in code in response to a button click i.e each button touch (click) will...
6
by: George | last post by:
Hi, I have been encountering a refresh problem with DataGridView, which is bound to a DataTable. When I make updates (Add, Delete, update) to the DataGridView, everything flow nicely to...
2
by: Lennart | last post by:
Hi I have a DataGridView on a Form filled with data from a DataTable. When I Load this form I want to select a row in the DataGridView depending on a selected Primary Key index; How do I do...
0
by: Paul Cheetham | last post by:
Hi, I have a DataGridView control, where I am setting its Datasource property to a datatable. The DataGridView has it's MultiSelect property set to false I also have a handler for the...
0
by: DBC User | last post by:
I have a datagridview and there are 4 coulmns, the user could do sort on any of the columns. After doing sorting. The user could do refresh the datagridview and get another set of data. But when I...
0
by: Andrus | last post by:
I use DataGridView containing DataGridViewComboBoxColumn column. Combobox column displays dropdown list opening selection triangle in all rows. This covers part of visible data if columns are...
7
by: cj | last post by:
As your probably aware the datagridview doesn't update it's source datatable with changes to a cell until you move off that cell. Suppose somone makes a change in a cell of my datagridview then...
2
by: colin | last post by:
Hi, I need to tell how many rows are visible on the screen, in my DataGridView control so that I can ensure my newly selected row is visible without doing any unecessry scrolling, I thought...
0
by: deathprincess | last post by:
Hi Guyz, Need some help in updating my data grid view. I used the Rows removed and selection changed events. i have a code on the selection changed events and it worked well but the code on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.