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();
        }
C#.NET Programming

Avatar of undefined
Last Comment
jackjohnson44

8/22/2022 - Mon
ASKER
jackjohnson44

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
Jacques Bourgeois (James Burger)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
jackjohnson44

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;
                }
            }
        }


    }
ASKER
jackjohnson44

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!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes