如何确定是正确的点击进行的ContextMenuStrip的dataGridView细胞? [英] How to identify dataGridView cell that was right clicked on for ContextMenuStrip?
问题描述
用户权限点击一个DGV内的单元格,然后做了一个的ContextMenuStrip选择。根据他们的CMS的选择,我想要做的事(复制,隐藏,过滤器)。我的问题是标识是正确的点击单元格中。
我试图处理这种情况有以下方法,但[参数:columnIndex]不能被引用。
私人无效cmsDataGridView_ItemClicked(对象发件人,ToolStripItemClickedEventArgs E)
{
开关(e.ClickedItem.Text )
{
案复制:
中断;
案过滤器的:
中断;
案隐藏列:
DataGridViewBand带= dataGridView1.Columns [e.ColumnIndex]
band.Visible = FALSE;
中断;
}
}
我应该在两种不同的方法这样做呢?一个处理鼠标点击(中,我可以再捕捉DGV列索引),然后从内部那里,我叫CMS项目点击事件?
感谢你的帮助,
布赖恩。
这为我的作品的代码。哦,我也必须从设计中的DataGridView的ContextMenuStrip属性中删除cmsDataGridView方法。 。离开,在那里引起的问题。
//确定小区点击了cmsDataGridView
的DataGridViewCell clickedCell;
私人无效dataGridView1_CellMouseClick(对象发件人,DataGridViewCellMouseEventArgs E)
{
试
{
如果(e.Button == MouseButtons.Right)
$ { b $ b dataGridView1.ClearSelection();
clickedCell = dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex]
clickedCell.Selected = TRUE;
cmsDataGridView.Show(dataGridView1,e.Location);
}
}
赶上(异常前)
{
Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
}
}
私人无效cmsDataGridView_ItemClicked(对象发件人,ToolStripItemClickedEventArgs E)
{
开关(e.ClickedItem.Text)
{
案复制:
中断;
案过滤器的:
中断;
案隐藏列:
DataGridViewBand带= dataGridView1.Columns [clickedCell.ColumnIndex]
band.Visible = FALSE;
中断;
}
}
您可以跟踪任何细胞最后点击通过添加在DataGridView的鼠标点击的事件处理程序。
是这样的:
的DataGridViewCell clickedCell;
私人无效dataGridView1_CellMouseClick_1(对象发件人,DataGridViewCellMouseEventArgs E)
{
试
{
DataGridView的观点=(DataGridView中)发送;
如果(e.Button == System.Windows.Forms.MouseButtons.Right&放大器;&安培; e.RowIndex> = 0)
{
Console.WriteLine(点击栏目
+ e.ColumnIndex +,行
+ e.RowIndex +的DataGridView的
+ view.Name +为
+ System.Windows。 Forms.Cursor.Position);
clickedCell = view.Rows [e.RowIndex] .Cells [e.ColumnIndex]
}
}
赶上(异常前)
{
Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
}
}
然后在你的contextMenuStripItem点击事件,在clickedCell开关。像值:
开关(clickedCell.Value)
{
案复制:
断点;
... //等
}
User right clicks on a cell within a DGV, then makes a selection in the ContextMenuStrip. Based on their CMS selection, I want to do something (copy, hide, filter). My problem is identifying the cell that was right clicked on.
I was trying to handle this scenario with the following method, but [ColumnIndex] cannot be referenced.
private void cmsDataGridView_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
switch (e.ClickedItem.Text)
{
case "Copy":
break;
case "Filter On":
break;
case "Hide Column":
DataGridViewBand band = dataGridView1.Columns[e.ColumnIndex];
band.Visible = false;
break;
}
}
Should I be doing this in two different methods? One to handle the mouse click (in which I could then capture the DGV column index), then from within there, I call the CMS item clicked event?
Thank you for your help, Brian.
The code that works for me. Oh and I did have to remove the cmsDataGridView method from the ContextMenuStrip property of the dataGridView within the designer. Leaving that in there caused problems.
// Identify the cell clicked for cmsDataGridView
DataGridViewCell clickedCell;
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
if (e.Button == MouseButtons.Right)
{
dataGridView1.ClearSelection();
clickedCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
clickedCell.Selected = true;
cmsDataGridView.Show(dataGridView1, e.Location);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
}
}
private void cmsDataGridView_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
switch (e.ClickedItem.Text)
{
case "Copy":
break;
case "Filter On":
break;
case "Hide Column":
DataGridViewBand band = dataGridView1.Columns[clickedCell.ColumnIndex];
band.Visible = false;
break;
}
}
You could keep track of whichever cell was last clicked by adding an event handler for the DataGridView's mouse click.
Something like:
DataGridViewCell clickedCell;
private void dataGridView1_CellMouseClick_1(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
DataGridView view = (DataGridView)sender;
if (e.Button == System.Windows.Forms.MouseButtons.Right && e.RowIndex >= 0)
{
Console.WriteLine("Clicked column "
+ e.ColumnIndex + ", row "
+ e.RowIndex + " of DataGridView "
+ view.Name + " at "
+ System.Windows.Forms.Cursor.Position);
clickedCell = view.Rows[e.RowIndex].Cells[e.ColumnIndex];
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
}
}
Then in your contextMenuStripItem click event, switch on clickedCell.Value like:
switch (clickedCell.Value)
{
case "Copy":
break;
... // etc.
}
这篇关于如何确定是正确的点击进行的ContextMenuStrip的dataGridView细胞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!