Mouse right click event for data grid view


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Mouse right click event for data grid view

  1. #1
    Join Date
    Feb 2010
    Posts
    17

    Mouse right click event for data grid view

    I have a data grid view and a context menu on a windows form. I was wondering if there is a way that if a user right clicked on the grid (on any cell on any row) that it would select the row and show the context menu?

    I already have the gird view set up for the context menu and it works, but can I have it to where it would also select the row too?

  2. #2
    Join Date
    Feb 2010
    Posts
    17
    Okay, so I just read what I wrote and I didn't clearly specify what I really needed to know.

    What I have: 1 data grid view; 1 context menu. I have DataGridView.ContextMenuStrip property set to ContextMenu.

    What I want to know: When a user 'special clicks' on a row, any row. I want that row to be selected and the context menu show. Some of the options in the context menu are dependent upon which row is selected. I want to know if that possible and if it is, how can I do that.

    There's got to be a better way then first selecting the row, then special clicking on it.
    Last edited by stevekapaun01; 02-27-2010 at 02:27 AM.

  3. #3
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    Adding a ContextMenu traps the MouseClick event of the grid, but not the MouseDown. You can thus select the row just before the ContextMenu opens by trapping the right click in the MouseDown event:

    Code:
    Private Sub dgvParts_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvParts.CellMouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            For Each row As DataGridViewRow In dgvParts.SelectedRows
                row.Selected = False
            Next
            dgvParts.Rows(e.RowIndex).Selected = True
        End If
    End Sub
    Since you seem to want only that row to be selected, you need to unselect the currently selected rows before selecting the new row. This is what the For loop does.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  4. #4
    Join Date
    Feb 2010
    Posts
    17
    Thanks JBourgeois for your reply, that exactly what I needed!

  5. #5
    Join Date
    Feb 2010
    Posts
    17
    Okay, so new question. How would one do the very same thing in C Sharp.

    I got all the way to deleting the rows, but I get an error when I try to select the rowindex.

    Code:
    private void dataGridView1_MouseDown(object sender, System.Windows.Forms.DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                row.Selected = false;
            }
            //need help for gridAlbums.Rows(e.RowIndex).Selected = True
        }
    }
    After the foreach, how would I do in C# that's in comment. I get an red line under Rows

  6. #6
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    Mapping VB to C# is pretty straightforward, as you seem to have found.

    What does the red line tells you when you move the cursor over it and get the tooltip about the problem?

    From what I see, there could be 2 problems, but I do not see firsthand why you have the error on the .Rows, I would expect them somewhere else. Red usually means a syntax error and the 2 problems I see would usually show as compile errors, blue instead of red.

    The first problem is that you loop into dataGridView1, but try to select a row in gridAlbums. Are both variables pointing to the same object? If not, you clear one grid and then select a row in the other????

    Second problem is that C# is case sensitive and does not have the same case syntax for True as VB does. So True is not good, you need to type true, without the uppercase at the beginning.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  7. #7
    Join Date
    Feb 2010
    Posts
    17
    The gridAlbums was just the name of the grid in my VB project, I just copied that one line into the comment.

    But anyway, I am somewhat knowlegable from VB and not so much in C#. i am taking a class this semester for it, I thought I could work it through VB then C#.

    I don't know what you would need to help me so here is my entire Form1.cs

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace DataGridView
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                dataGridView1.Rows.Add(5);
            }
    
            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
            }
            private void dataGridView1_MouseDown(object sender, System.Windows.Forms.DataGridViewCellMouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                    {
                        row.Selected = false;
                    }
                    dataGridView1.Rows(e.RowIndex).Selected = true;
                }
            }
        }
    }
    I also change my Form1.Designer.cs for this method
    Code:
    this.dataGridView1.CellMouseDown += new System.Windows.Forms.DataGridViewCellMouseEventHandler(dataGridView1_MouseDown);
    with the "dataGridView1.Rows(e.RowIndex).Selected = true;" I get the red line just under Rows and my error message is: "Non-invocable member 'System.Windows.Forms.DataGridView.Rows' cannot be used like a method."

  8. #8
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    Oops.

    Since I do C# only once in a while, and have done none in the last month, my reflexes are not very sharp in that language. Sorry, it took an extra post to light my brain from the proper angle. With the error message, it's clear now.

    In VB you use parenthesis () both to call methods and define the indexes in collection and arrays. C# uses () only for call to methods. You get to collection and array elements with square brackets [].

    In VB, you thus call Rows(3). In C#, it should be Rows[3].

    This is one of those little things you have to "square" when you copy from VB to C#.

    Since you left the parenthesis after your copy, the C# compiler thought that you were trying to call a method, and since there is no Row method, you got your red error, which clearly points to the problem, although the text of the message is not as explicit as it could be. Don't feel bad if you make that mistake from time to time, everytime I start working in C#, I need to have 2 or 3 warnings before my mind switch off the automatic () that for me comes natural with collections.

    Simply go like this, and I am quite sure it will work:

    Code:
    dataGridView1.Rows[e.RowIndex].Selected = true
    -------

    A little out of topic, but I cannot resist.

    And I do not want to start a war language here, C# and VB are equals as far as possibilities and performances are concerned. So is COBOL.Net and Python.Net. The one that does the job is the framework, and we all use the same framework. In .NET (this is not necessarily the case in other environments) a language is nothing but an interface to the framework. The syntax might be different, the job done ends up being exactly the same.

    I have a solid but a little outdated C++ backgound (so to me C# is not an alien language), but have spent most my 90's in VB classic (I clearly made a choice when I switched from C++ to VB). I can tell you that in the .NET environment, VB as a clear advantage, not really as a language, but for the way the editor works. I am 10 to 15% more productive in VB than in C# just because of the way the editor / debugger / compiler work together.

    Nothing beats knowing and working in both (and more than both if you can). What you learn in one language is always useful in others, so go on with your learning of C#, it will serve you, if not only by the fact that a lot employers tends to consider that C# programmers are better (which is not really true, although it is true that it is easier to write bad code in VB than it is in C#), and you might get a better paycheck. So, if you are in it for the money, lean toward C#.

    For people who have a strong C, C++ or Java background, what I will tell you is probably not true. But if you are beginning and/or feel more at ease in VB (not case sensitive, no stupid ; at the end of each line, and a few others), if you program because you love it over of because you want to buy a new Corvette every 2 years, lean toward VB. You will have a lot more fun programming, and will end up having the framework do the exact same thing than having to work with a fussy editor, take care of case, find if the } goes with the if, the select or the for, and a few others.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  9. #9
    Join Date
    Feb 2010
    Posts
    17
    Thanks, you have been a big help for me.

  10. #10
    Join Date
    Jun 2014
    Posts
    1

    Datagridview right click

    private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    {
    if (e.Button == MouseButtons.Right)
    {
    this.dataGridView1.Rows[e.RowIndex].Selected = true;
    this.rowIndex = e.RowIndex;
    this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1];
    this.contextMenuStrip1.Show(this.dataGridView1, e.Location);
    contextMenuStrip1.Show(Cursor.Position);
    }
    }

    Full c# source....Datagridview right click and delete

    watson

    Quote Originally Posted by stevekapaun01 View Post
    I have a data grid view and a context menu on a windows form. I was wondering if there is a way that if a user right clicked on the grid (on any cell on any row) that it would select the row and show the context menu?

    I already have the gird view set up for the context menu and it works, but can I have it to where it would also select the row too?

Similar Threads

  1. Replies: 2
    Last Post: 10-16-2007, 07:43 PM
  2. Replies: 1
    Last Post: 08-10-2006, 09:22 AM
  3. Tree view on mouse over
    By anbaz in forum ASP.NET
    Replies: 0
    Last Post: 06-21-2005, 06:12 AM
  4. Replies: 0
    Last Post: 06-20-2002, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center