?
Solved

DatagridView Selection with right click for contextmenu selection C# vb.net ado.net

Posted on 2008-11-18
3
Medium Priority
?
4,915 Views
Last Modified: 2013-11-07
Ok, i have a non editable datagridview control on my windows form. I have it setup so that each row can be selected. When the user right clicks on the datagridview i have a context menu appear. The only problem is that i want the correct row to be selected on the right click.

So lets say that the first row is selected when the form loads, the user right clicks on the fourth row, i want the forth row of the grid to become selected as the context menu appears with the options. Currently the only way the row will select is when the user left clicks on the row.

I will attach the markup from the designer.

Thanks in advance.

'
        'dgvCustomers
        '
        Me.dgvCustomers.AllowUserToAddRows = False
        Me.dgvCustomers.AllowUserToDeleteRows = False
        Me.dgvCustomers.AllowUserToOrderColumns = False
        Me.dgvCustomers.AllowUserToResizeColumns = False
        Me.dgvCustomers.AllowUserToResizeRows = False
        Me.dgvCustomers.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.RaisedVertical
        Me.dgvCustomers.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing
        Me.dgvCustomers.ColumnHeadersVisible = False
        Me.dgvCustomers.ContextMenuStrip = Me.ContextMenuStrip1
        Me.dgvCustomers.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically
        Me.dgvCustomers.Location = New System.Drawing.Point(12, 385)
        Me.dgvCustomers.MultiSelect = False
        Me.dgvCustomers.Name = "dgvCustomers"
        Me.dgvCustomers.RowHeadersVisible = False
        Me.dgvCustomers.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing
        Me.dgvCustomers.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.[False]
        Me.dgvCustomers.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.dgvCustomers.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
        Me.dgvCustomers.Size = New System.Drawing.Size(505, 172)
        Me.dgvCustomers.TabIndex = 3

Open in new window

0
Comment
Question by:talker2004
3 Comments
 
LVL 10

Accepted Solution

by:
kdwood earned 2000 total points
ID: 22986192
Greetings talker,

Try this on your MouseDown event:


Hope this helps,

Keith

 
Private Sub dgvCustomers_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvCustomers.MouseDown
 
 
        If e.Button = Windows.Forms.MouseButtons.Right Then
 
            Dim hti As DataGridView.HitTestInfo = sender.HitTest(e.X, e.Y)
 
            If hti.Type = DataGridViewHitTestType.Cell Then
 
                If Not Me.dgvCustomers.Rows(hti.RowIndex).Selected Then
 
                    ' User right clicked a row that is not selected, so throw away all other selections and select this row
 
                    Me.dgvCustomers.ClearSelection()
                    Me.dgvCustomers.Rows(hti.RowIndex).Selected = True
 
                End If
 
            End If
 
        End If
 
   End Sub

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22986236
0
 
LVL 7

Author Closing Comment

by:talker2004
ID: 31517898
Worked on my first try. Thanks a bunch....
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question