DatagridView Selection with right click for contextmenu selection C#

Posted on 2008-11-18
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.




        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

Question by:talker2004
    LVL 10

    Accepted Solution

    Greetings talker,

    Try this on your MouseDown event:

    Hope this helps,


    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.Rows(hti.RowIndex).Selected = True
                    End If
                End If
            End If
       End Sub

    Open in new window

    LVL 48

    Expert Comment

    LVL 7

    Author Closing Comment

    Worked on my first try. Thanks a bunch....

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now