Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. DataGridView selection on right click

DataGridView selection on right click

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Johnny 0
    wrote on last edited by
    #1

    :confused: I have a DataGridView object and I added a ContextMenuStrip control to the DataGridView.ContextMenu. This works well, if I right-click the datagrid view I get the context menu I wanted. The problem is, I would like to, just before the context menu appears perform a left click to also select the cell or row under the mouse at that moment. Not sure what is the best way to do this. Thanks.

    A 1 Reply Last reply
    0
    • J Johnny 0

      :confused: I have a DataGridView object and I added a ContextMenuStrip control to the DataGridView.ContextMenu. This works well, if I right-click the datagrid view I get the context menu I wanted. The problem is, I would like to, just before the context menu appears perform a left click to also select the cell or row under the mouse at that moment. Not sure what is the best way to do this. Thanks.

      A Offline
      A Offline
      Andrew Lygin
      wrote on last edited by
      #2

      Hi, Johnny. This CellMouseDown event handler should work:

      private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
      {
      if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
      {
      dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
      }
      }

      J 1 Reply Last reply
      0
      • A Andrew Lygin

        Hi, Johnny. This CellMouseDown event handler should work:

        private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
        if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
        {
        dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
        }
        }

        J Offline
        J Offline
        Johnny 0
        wrote on last edited by
        #3

        WOW, that was so simple, thanks a million times Andrew

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups