Advertisement

02.09.2008 at 04:44PM PST, ID: 23150767
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Right click and select datagridview row
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?
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: wiswalld
Solution Provided By: jpaulino
Participating Experts: 2
Solution Grade: A
Views: 218
Translate:
Loading Advertisement...
02.09.2008 at 04:52PM PST, ID: 20859439

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 05:07PM PST, ID: 20859496

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 05:19PM PST, ID: 20859532

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 05:23PM PST, ID: 20859551

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 05:27PM PST, ID: 20859567

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 05:36PM PST, ID: 20859610

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 05:43PM PST, ID: 20859632

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 05:51PM PST, ID: 20859671

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 05:56PM PST, ID: 20859693

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 06:02PM PST, ID: 20859705

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 06:06PM PST, ID: 20859719

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 06:13PM PST, ID: 20859739

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 06:14PM PST, ID: 20859744

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 06:21PM PST, ID: 20859756

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.10.2008 at 02:05AM PST, ID: 20860678

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 04:00PM PST, ID: 20871524

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 04:00PM PST, ID: 20871528

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.09.2008 at 04:52PM PST, ID: 20859439

Rank: Sage

Hi wiswalld,

Do you have the context menu set it on the datagridview properties ?
 
02.09.2008 at 05:07PM PST, 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
 
02.09.2008 at 05:19PM PST, ID: 20859532

Rank: Sage

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
Accepted Solution
 
02.09.2008 at 05:23PM PST, ID: 20859551
Shows menu strip but does not hightlight row.
 
02.09.2008 at 05:27PM PST, ID: 20859567

Rank: Sage

>> Shows menu strip but does not hightlight row.
But hightlight the cell, right ?
 
02.09.2008 at 05:36PM PST, 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?
 
02.09.2008 at 05:43PM PST, ID: 20859632

Rank: Sage

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
 
02.09.2008 at 05:51PM PST, 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.
 
02.09.2008 at 05:56PM PST, ID: 20859693

Rank: Sage

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
 
02.09.2008 at 06:02PM PST, 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.
 
02.09.2008 at 06:06PM PST, ID: 20859719

Rank: Sage

>> 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()
 
02.09.2008 at 06:13PM PST, ID: 20859739

Rank: Sage

Sorry I have to leave now for some hours. I will check latter if it's already solved or if you still need help.
 
02.09.2008 at 06:14PM PST, ID: 20859744
>>Add this before the for each loop


Not sure what you mean.
 
02.09.2008 at 06:21PM PST, ID: 20859756
I am going to bed. Check back tomorrow.
 
02.10.2008 at 02:05AM PST, ID: 20860678

Rank: Sage

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
 
02.11.2008 at 04:00PM PST, ID: 20871524
It is succesfully highlighting the row I want with a right click but still copying the first row not the selected row.
 
02.11.2008 at 04:00PM PST, 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.
 
 
03.06.2008 at 03:11AM PST, 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):
1:
2:
3:
4:
5:
6:
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
 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628