[Mono-bugs] [Bug 462638] Set a ContexMenu to DatagridviewColumn do nothing

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Tue Mar 22 21:07:45 EDT 2011


https://bugzilla.novell.com/show_bug.cgi?id=462638

https://bugzilla.novell.com/show_bug.cgi?id=462638#c2


Allen Kempe <allenck at windstream.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |allenck at windstream.net

--- Comment #2 from Allen Kempe <allenck at windstream.net> 2011-03-23 01:07:44 UTC ---
Here is some code that works: 

You need to add an eventhandler for CellMouseEnter to capture the mouse
location. Also, add an event handler for ContextMenyStrip.Opening. 

       private void AddContextMenu()
        {
            toolStripItem1.Text = "Copy";
            toolStripItem1.Click += new EventHandler(toolStripItem1_Click);
            toolStripItem2.Text = "Hide Column";
            toolStripItem2.Click += new EventHandler(toolStripItem2_Click);
            ContextMenuStrip strip = new ContextMenuStrip();
            strip.Items.AddRange(new ToolStripItem[]{toolStripItem1,
toolStripItem2});
            this.CellMouseEnter += new
DataGridViewCellEventHandler(DataGridViewEx_CellMouseEnter);
            foreach (DataGridViewColumn column in this.Columns)
            {

                column.ContextMenuStrip = strip;
                //column.ContextMenuStrip.Items.Add(toolStripItem1);

            }
            this.ContextMenuStrip = strip;
            this.ContextMenuStrip.Opening +=new
System.ComponentModel.CancelEventHandler(ContextMenuStrip_Opening);
        }

        void toolStripItem2_Click(object sender, EventArgs e)
        {
            this.Columns[mouseLocation.ColumnIndex].Visible = false;
        }

        void ContextMenuStrip_Opening(object sender,
System.ComponentModel.CancelEventArgs e)
        {
            // We don't want a context menu unless the mouse is over an actual
cell. 
            if (mouseLocation.ColumnIndex == -1 || mouseLocation.RowIndex ==
-1)
            {
                e.Cancel = true;
                return;
            }
            // select the cell to highlight it
            this[mouseLocation.ColumnIndex, mouseLocation.RowIndex].Selected =
true;
        }
        // Deal with hovering over a cell.

        void DataGridViewEx_CellMouseEnter(object sender,
DataGridViewCellEventArgs e)
        {
            mouseLocation = e;
        }

         private DataGridViewCellEventArgs mouseLocation;

        // Copy Data to the Clipboard
        private void toolStripItem1_Click(object sender, EventArgs args)
        {
            //this.Rows[mouseLocation.RowIndex]
            //    .Cells[mouseLocation.ColumnIndex].Style.BackColor
            //    = Color.Red;
            Clipboard.SetDataObject(this[mouseLocation.ColumnIndex,
mouseLocation.RowIndex].Value.ToString());
        }

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list