Link to home
Start Free TrialLog in
Avatar of jackjohnson44
jackjohnson44

asked on

c# how do I get the rowindex from a contextmenu?

I have a context menu with an event handler for the menu item.  I can get into it, but I can't get the rowindex that was clicked on.

        public static void DataGridViewContextMenuOptionChain(this DataGridView dataGrid)
        {
            ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
            ToolStripMenuItem toolStripToggleActive = new ToolStripMenuItem("Toggle Active");
            ToolStripMenuItem toolStripDeleteChain = new ToolStripMenuItem("Delete Chain");

            toolStripToggleActive.Click += toolStripToggleActive_Click;
            toolStripDeleteChain.Click +=toolStripDeleteChain_Click;
            contextMenuStrip.Items.AddRange(new ToolStripItem[] { toolStripToggleActive, toolStripDeleteChain});
            dataGrid.ContextMenuStrip = contextMenuStrip;
        }

        private static void toolStripDeleteChain_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
Avatar of jackjohnson44
jackjohnson44

ASKER

One more thing to mention is that I am adding the above as an extension method in a static class to reuse it.  I am not adding this to a form, so I can't just make a reference to a hard coded grid in the event handlers.
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!


I ended up creating a class for the context menu item which contains the creating as well as events for the context menu.  I use a variation of your code to grab the row index.

I just create an instance of the object on my main form and pass in the datagrid.  The best part is that I can associate it with any grid that has the same column ID.  I have several similar grids on other pages.

How I call it:
                var contextMenuController = new ContextMenuDataGridSpread(dataGrid, _currentChain);

The class:

    public class ContextMenuDataGridSpread
    {
        private DataGridView _dataGridView;
        private int _rowIndex = -1;
        private OrderChain _orderChain;

        public ContextMenuDataGridSpread(DataGridView dataGrid, OrderChain orderChain)
        {
            _dataGridView = dataGrid;
            _orderChain = orderChain;
            CreateContextMenu(dataGrid);
        }

        private void CreateContextMenu(DataGridView dataGrid)
        {
            ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
            ToolStripMenuItem toolStripEdit = new ToolStripMenuItem("Edit Spread");
            toolStripEdit.Click += toolStripEdit_Click;
            contextMenuStrip.Items.AddRange(new ToolStripItem[] { toolStripEdit});
            dataGrid.ContextMenuStrip = contextMenuStrip;
            dataGrid.MouseDown += dataGrid_MouseDown;
        }

        void toolStripEdit_Click(object sender, EventArgs e)
        {
            var spreadId = _dataGridView.GetStringValFromSelRow("ID");
            var currentSpread = _orderChain.GetSpreadBySpreadId(spreadId);

            using (var form = new FormInputComplex(currentSpread))
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    _orderChain.Save();
                }
            }
        }

        public void dataGrid_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                DataGridView.HitTestInfo hit = _dataGridView.HitTest(e.X, e.Y);
                if (hit.Type == DataGridViewHitTestType.Cell)
                {
                    _rowIndex = hit.RowIndex;
                }
                else
                {
                    _rowIndex = -1;
                }
            }
        }


    }
I've requested that this question be closed as follows:

Accepted answer: 0 points for jackjohnson44's comment #a39473938
Assisted answer: 500 points for JamesBurger's comment #a39472539

for the following reason:

Thanks!