Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6366125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:22:57+00:00 2026-05-25T00:22:57+00:00

In my code below, I’m showing a context menu when the user right-clicks on

  • 0

In my code below, I’m showing a context menu when the user right-clicks on a cell in my DataGridView. I’d also like the cell that the user right-clicks on to change background color so that they can see the cell they’ve “right-click selected”. Is there a way to add something to my code below so that this occurs?

private void dataGridView2_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            ContextMenu m = new ContextMenu();
            MenuItem mnuCopy = new MenuItem("Copy");
            mnuCopy.Click += new EventHandler(mnuCopy_Click);
            m.MenuItems.Add(mnuCopy);

            int currentMouseOverRow = dataGridView2.HitTest(e.X, e.Y).RowIndex;               

            m.Show(dataGridView2, new Point(e.X, e.Y));

        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:22:58+00:00Added an answer on May 25, 2026 at 12:22 am

    So obviously you’ve hacked into my workstation and have seen some of the stuff I’ve worked on recently. I exaggerate a bit because I didn’t do exactly what you’re trying to do but with a little bit of tweaking I was able to.

    I would modify your MouseClick event to get the DGV’s CurrentCell. Once you have it, set the CurrentCell‘s Style property with the SelectionBackColor you want. Something like this:

    // ...
    DataGridView.HitTestInfo hti = dataGridView2.HitTest(e.X, e.Y);
    if (hti.Type == DataGridViewHitTestType.Cell) {
        dataGridView2.CurrentCell = dataGridView2.Rows[hti.RowIndex].Cells[hti.ColumnIndex];
        dataGridView2.CurrentCell.Style = new DataGridViewCellStyle { SelectionBackColor = System.Drawing.Color.Yellow};
    }
    //...
    

    The above is a bit ‘air code-y’ (in other words I haven’t attempted to merge it with your code and run it) but I hope you get the idea. Notice that I check through the hit test that a cell was clicked; if you don’t do this and the user does not click a cell you might have some problems.

    Now there’s the problem that this code will change the SelectionBackColor for the all the cells you right click. That’s easy to restore this property in the DGV’s CellLeave event:

    private void dgvBatches_CellLeave(object sender, DataGridViewCellEventArgs e) {
        dataGridView2.CurrentCell.Style = new DataGridViewCellStyle { SelectionBackColor = System.Drawing.SystemColors.Highlight };
    }
    

    I’ll have to remember this visual affect; thanks for asking the question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below shows a sample that I've used recently to explain the different
code below. i'm tryind to obtain string answers like a1, c4 this is what
Code below is a simple c# function return an int array. I would like
The code below is incorrect but the idea here is that I want to
The code below explains best what I'm trying to accomplish. I know that I
The code below illustrates the destruct() being called twice. I'd like to know why?
The code below shows a small WinForms app which includes a simple Control that
The code below obviously searches a directory for Files that contain the word FINAL
The code below does what I need it to do, but it looks like
The code below only outputs single line(there are 2 in database that should be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.