[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Right click and select datagridview row

Posted on 2008-02-09
19
Medium Priority
?
4,015 Views
Last Modified: 2012-06-21
I have a datagridview with a context menu strip attached. When a user selects a row and right clicks they get a menu to perform a task. Why do they have to left click a row to select before right clicking to perform the menu task. How do i fix this?
0
Comment
Question by:wiswalld
19 Comments
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20859439
Hi wiswalld,

Do you have the context menu set it on the datagridview properties ?
0
 
LVL 13

Author Comment

by:wiswalld
ID: 20859496
Yes

This is the code I run with one of the menu items from the context menu strip:

 Private Sub AssignUnitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AssignUnitToolStripMenuItem.Click

        Dim dr As DataRow = Me._CAD_050107DataSet.Tables("assignedvehicles").NewRow
        'Put values into row
        dr(1) = EventNumber.Text
        dr(2) = DataGridView1.CurrentRow.Cells(5).Value.ToString
        dr(3) = DataGridView1.CurrentRow.Cells(2).Value.ToString
        dr(4) = DataGridView1.CurrentRow.Cells(3).Value.ToString
        Me.DataGridView1.CurrentRow.Cells(7).Value = "No"
        Me.DataGridView1.CurrentRow.Cells(7).Style.BackColor() = Color.Red
        'and so on, making sure you put values in any ...
        '... fields/columns that do not allow null
        Me._CAD_050107DataSet.Tables("assignedvehicles").Rows.Add(dr)
        Me.DataGridView1.CurrentRow.Cells(7).Value = "No"
        Me.DataGridView1.CurrentRow.Cells(7).Style.BackColor() = Color.Red
        Me.Validate()
        Me.AssignedVehiclesBindingSource.EndEdit()
        Me.DataGridView2.EndEdit()
        Me.AssignedVehiclesTableAdapter.Update(Me._CAD_050107DataSet.AssignedVehicles)
        UpdateDataBase()








    End Sub
0
 
LVL 48

Accepted Solution

by:
Jorge Paulino earned 500 total points
ID: 20859532
Well normally, without any code, you cannot select a datagridview cell with the right click. So you have to select first the cell and then call the contex menu.

Try this:

    Private Sub DataGridView1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then

            Dim hti As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y)
            If hti.Type = DataGridViewHitTestType.Cell And hti.RowIndex >= 0 Then

                ' Selects the datagridview cell
                DataGridView1.CurrentCell = DataGridView1(hti.ColumnIndex, hti.RowIndex)

                ' Show the context menu
                ContextMenuStrip1.Show(Me.DataGridView1, New Point(e.X, e.Y))

            End If

        End If
    End Sub
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 13

Author Comment

by:wiswalld
ID: 20859551
Shows menu strip but does not hightlight row.
0
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20859567
>> Shows menu strip but does not hightlight row.
But hightlight the cell, right ?
0
 
LVL 13

Author Comment

by:wiswalld
ID: 20859610
Yes. I didn't try that. I am trying to figure out how to highlight the row by right clicking the datagridview border. Is that possible?
0
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20859632
Sure:

 Private Sub DataGridView1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then

            Dim hti As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y)
            If hti.Type = DataGridViewHitTestType.RowHeader And hti.RowIndex >= 0 Then

                ' Selects the datagridview row
                DataGridView1.Rows(hti.RowIndex).Selected = True

                ' Show the context menu
                ContextMenuStrip1.Show(Me.DataGridView1, New Point(e.X, e.Y))

            End If

        End If
    End Sub
0
 
LVL 13

Author Comment

by:wiswalld
ID: 20859671
The only problem I have is when I open the form the first row first cell is highlighted and if I right click the second row it highlights fine but copies the first row not the second row.
0
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20859693
Ok I see ... no problem. Try this:

 Private Sub DataGridView1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then

            Dim hti As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y)
            If hti.Type = DataGridViewHitTestType.RowHeader And hti.RowIndex >= 0 Then

               ' Clears other selections
                For Each row As DataGridViewRow In DataGridView1.Rows
                    DataGridView1.Rows(row.Index).Selected = False
                Next

                ' Selects the datagridview row
                DataGridView1.Rows(hti.RowIndex).Selected = True

                ' Show the context menu
                ContextMenuStrip1.Show(Me.DataGridView1, New Point(e.X, e.Y))

            End If

        End If
    End Sub
0
 
LVL 13

Author Comment

by:wiswalld
ID: 20859705
Getting really warm. Highlight the selected row without highlighting the first cell in the first row but still copies the first row unless I left click the second row first.

I can hide the row header and just use the first suggestion but was trying to avoid that.
0
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20859719
>> Getting really warm. Highlight the selected row without highlighting the first cell in the first row but still copies the first row unless I left click the second row first.

thats probably because that cell is in edit mode.

Add this before the for each loop

  DataGridView1.EndEdit()
0
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20859739
Sorry I have to leave now for some hours. I will check latter if it's already solved or if you still need help.
0
 
LVL 13

Author Comment

by:wiswalld
ID: 20859744
>>Add this before the for each loop


Not sure what you mean.
0
 
LVL 13

Author Comment

by:wiswalld
ID: 20859756
I am going to bed. Check back tomorrow.
0
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20860678
This way:

Private Sub DataGridView1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then

            Dim hti As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y)
            If hti.Type = DataGridViewHitTestType.RowHeader And hti.RowIndex >= 0 Then

                DataGridView1.EndEdit()

               ' Clears other selections
                For Each row As DataGridViewRow In DataGridView1.Rows
                    DataGridView1.Rows(row.Index).Selected = False
                Next

                ' Selects the datagridview row
                DataGridView1.Rows(hti.RowIndex).Selected = True

                ' Show the context menu
                ContextMenuStrip1.Show(Me.DataGridView1, New Point(e.X, e.Y))

            End If

        End If
    End Sub
0
 
LVL 13

Author Comment

by:wiswalld
ID: 20871524
It is succesfully highlighting the row I want with a right click but still copying the first row not the selected row.
0
 
LVL 13

Author Comment

by:wiswalld
ID: 20871528
I appreciate the help but looks like I may have to go with the first choice and eliminate the row header and select the cell. Didn't really want to do that.
0
 
LVL 2

Expert Comment

by:ab_khadilkar
ID: 21059038
This is code for c#

This works, although you might need some more error-checking and it only selects the cell  It also assign context menu to that cell

But, here's what I did. In the event handler for the Grid's CellMouseDown event, put this code (hopefully you won't mind  C# code here ... it should be an easy translation for you):
private void DataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
 
ContextMenu.Show(DataGridView1, new Point(e.RowIndex, e.ColumnIndex));
 
}

Open in new window

0
 
LVL 3

Expert Comment

by:Reset_
ID: 35511831
a bit dirty perhaps but it does the trick :)

The problem is that the vb.code that is accepted as the solution always return rowindex 0. If you do a select on a cell the row index gets updated.
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
                'MsgBox("HTI.type = Cell")
                If Not Me.NoderDataGridView.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.NoderDataGridView.EndEdit()
                    Me.NoderDataGridView.ClearSelection()

                    Me.NoderDataGridView.Rows(hti.RowIndex).Selected = True

                    NoderDataGridView.Rows(hti.RowIndex).Cells(2).Selected = True
                    NoderDataGridView.Rows(hti.RowIndex).Cells(3).Selected = True
                    NoderDataGridView.Rows(hti.RowIndex).Cells(2).Selected = True
                    

                End If

            End If

        End If

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

608 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