输入ContextMenuStrip_Click事件后,获取列索引 [英] Get column index after entering ContextMenuStrip_Click event
问题描述
private void dataGridView1_MouseDown(object sender,MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
var ht = dataGridView1.HitTest(eX,eY);
if((ht.Type == DataGridViewHitTestType.ColumnHeader))
{
contextMenuStrip1.Show(MousePosition);
}
}
}
目标: / strong>我希望能够在showToolStripMenuItem_Click事件中获取列索引(我右键单击并随后将上下文菜单打开)。这是这个事件的代码。
private void showToolStripMenuItem_Click(object sender,EventArgs e)
{
var ht = dataGridView1.HitTest(Cursor.Position.X,Cursor.Position.Y);
DataGridViewCellMouseEventArgs args = new
DataGridViewCellMouseEventArgs(ht.ColumnIndex,0,0,0,new
MouseEventArgs(System.Windows.Forms.MouseButtons.Right,0,0,0,0)) ;
dataGridView1_ColumnHeaderMouseClick(null,args);
}
不幸的是,showToolStripMenuItem_Click事件有EventArgs,而不是DataGridViewCellMouseEventArgs,在这种情况下很容易得到列索引:e.ColumnIndex。
你在第二个函数中看到的是我试图实例化一个DataGridViewCellMouseEventArgs类,并使用它来获取我的索引,不幸的是它的e.ColumnIndex属性总是返回-1。
我该怎么做?
CellContextMenuStripNeeded
事件 DataGridView
。
创建一个 ContextMenuStrip
然后处理 CellContextMenuStripNeeded
事件 DataGridView
并检查是否列标题,显示上下文菜单。
您可以使用 e.RowIndex
和 e检测行索引和列索引.ColumnIndex
。
代码
private void dataGridView1_CellContextMenuStripNeeded(object sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
//检查这是否是列标题
if(e.RowIndex == - 1)
{
//显示上下文菜单
e.ContextMenuStrip = this.contextMenuStrip1;
//e.ColumnIndex是您右键点击的列。
}
}
I have a simple datagridview, and I've added a context menu strip, with one choice. This context menu strip which would normally appear if you right clicked anywhere on the control, but with this code, I've made it to show only when you right click on the column header.
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var ht = dataGridView1.HitTest(e.X, e.Y);
if ((ht.Type == DataGridViewHitTestType.ColumnHeader))
{
contextMenuStrip1.Show(MousePosition);
}
}
}
Goal : I would like to be able to get the column index (that I right clicked and subsequently brought the context menu up) in the showToolStripMenuItem_Click event. This is the code for this event.
private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
var ht = dataGridView1.HitTest(Cursor.Position.X, Cursor.Position.Y);
DataGridViewCellMouseEventArgs args = new
DataGridViewCellMouseEventArgs(ht.ColumnIndex, 0, 0, 0, new
MouseEventArgs(System.Windows.Forms.MouseButtons.Right, 0, 0, 0, 0));
dataGridView1_ColumnHeaderMouseClick(null, args);
}
Unfortunately the showToolStripMenuItem_Click event has EventArgs, instead of DataGridViewCellMouseEventArgs, in which case it would be easy to get the column index: e.ColumnIndex.
What you see in the second function is I've tried to instantiate a DataGridViewCellMouseEventArgs class and use it to get my index, unfortunately it the e.ColumnIndex property here always returns -1.
How can I go about this?
You can use CellContextMenuStripNeeded
event of DataGridView
.
Create a ContextMenuStrip
and then handle CellContextMenuStripNeeded
event of DataGridView
and check if this is the column header, show context menu.
There you can detect the row index and column index using e.RowIndex
and e.ColumnIndex
.
Code
private void dataGridView1_CellContextMenuStripNeeded(object sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
//Check if this is column header
if (e.RowIndex == -1)
{
//Show the context menu
e.ContextMenuStrip = this.contextMenuStrip1;
//e.ColumnIndex is the column than you right clicked on it.
}
}
这篇关于输入ContextMenuStrip_Click事件后,获取列索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!