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 DataGridViewContextMenuOpt ionChain(t his DataGridView dataGrid)
{
ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
ToolStripMenuItem toolStripToggleActive = new ToolStripMenuItem("Toggle Active");
ToolStripMenuItem toolStripDeleteChain = new ToolStripMenuItem("Delete Chain");
toolStripToggleActive.Clic k += toolStripToggleActive_Clic k;
toolStripDeleteChain.Click +=toolStripDeleteChain_Cli ck;
contextMenuStrip.Items.Add Range(new ToolStripItem[] { toolStripToggleActive, toolStripDeleteChain});
dataGrid.ContextMenuStrip = contextMenuStrip;
}
private static void toolStripDeleteChain_Click (object sender, EventArgs e)
{
throw new NotImplementedException();
}
public static void DataGridViewContextMenuOpt
{
ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
ToolStripMenuItem toolStripToggleActive = new ToolStripMenuItem("Toggle Active");
ToolStripMenuItem toolStripDeleteChain = new ToolStripMenuItem("Delete Chain");
toolStripToggleActive.Clic
toolStripDeleteChain.Click
contextMenuStrip.Items.Add
dataGrid.ContextMenuStrip = contextMenuStrip;
}
private static void toolStripDeleteChain_Click
{
throw new NotImplementedException();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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( DataGridVi ew dataGrid, OrderChain orderChain)
{
_dataGridView = dataGrid;
_orderChain = orderChain;
CreateContextMenu(dataGrid );
}
private void CreateContextMenu(DataGrid View dataGrid)
{
ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
ToolStripMenuItem toolStripEdit = new ToolStripMenuItem("Edit Spread");
toolStripEdit.Click += toolStripEdit_Click;
contextMenuStrip.Items.Add Range(new ToolStripItem[] { toolStripEdit});
dataGrid.ContextMenuStrip = contextMenuStrip;
dataGrid.MouseDown += dataGrid_MouseDown;
}
void toolStripEdit_Click(object sender, EventArgs e)
{
var spreadId = _dataGridView.GetStringVal FromSelRow ("ID");
var currentSpread = _orderChain.GetSpreadBySpr eadId(spre adId);
using (var form = new FormInputComplex(currentSp read))
{
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.Ce ll)
{
_rowIndex = hit.RowIndex;
}
else
{
_rowIndex = -1;
}
}
}
}
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(
The class:
public class ContextMenuDataGridSpread
{
private DataGridView _dataGridView;
private int _rowIndex = -1;
private OrderChain _orderChain;
public ContextMenuDataGridSpread(
{
_dataGridView = dataGrid;
_orderChain = orderChain;
CreateContextMenu(dataGrid
}
private void CreateContextMenu(DataGrid
{
ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
ToolStripMenuItem toolStripEdit = new ToolStripMenuItem("Edit Spread");
toolStripEdit.Click += toolStripEdit_Click;
contextMenuStrip.Items.Add
dataGrid.ContextMenuStrip = contextMenuStrip;
dataGrid.MouseDown += dataGrid_MouseDown;
}
void toolStripEdit_Click(object
{
var spreadId = _dataGridView.GetStringVal
var currentSpread = _orderChain.GetSpreadBySpr
using (var form = new FormInputComplex(currentSp
{
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,
if (hit.Type == DataGridViewHitTestType.Ce
{
_rowIndex = hit.RowIndex;
}
else
{
_rowIndex = -1;
}
}
}
}
ASKER
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!
Accepted answer: 0 points for jackjohnson44's comment #a39473938
Assisted answer: 500 points for JamesBurger's comment #a39472539
for the following reason:
Thanks!
ASKER