C# - DatagridView和ContextMenu [英] C# - DatagridView and ContextMenu

查看:269
本文介绍了C# - DatagridView和ContextMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,我看到有关产品的信息。我想绑定一个上下文菜单,当用户选择一个单元格,然后右键单击该单元格。我有另一个上下文菜单,并且绑定到datagridview的列。如果用户右键单击上下文菜单显示的列。



我已经尝试了这个,但它不起作用。上下文菜单显示用户右键单击单元格,但是绑定到列标题的上下文菜单不起作用。

  private void GridView1_CellMouseUp(object sender,DataGridViewCellMouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
productContextMenu.Show(GridView1,e.Location);
}

}

如何让它当用户右键单击datagridview时显示?



提前提供多个。



编辑



Thnx为答案。我解决了这样的问题:

  private void GridView1_MouseUp(object sender,MouseEventArgs e)
{
DataGridView.HitTestInfo hitTestInfo;
if(e.Button == MouseButtons.Right)
{
hitTestInfo = GridView1.HitTest(e.X,e.Y);
if(hitTestInfo.Type == DataGridViewHitTestType.Cell)
{
productContextMenu.Show(GridView1,e.Location);
}

}
}

上下文显示。当我点击上下文菜单显示的列,当我点击上下文菜单显示的单元格。

解决方案

尝试这个

  private void dataGridView1_CellMouseDown(object sender,MouseEventArgs e)
{
if(e.Button == MouseButtons .Right)
{
contextMenu.Show(datagridview,e.Location);
}

}

  private void dataGridView_MouseUp(object sender,MouseEventArgs e)
{
//右键单击加载上下文菜单
DataGridView.HitTestInfo hitTestInfo;
if(e.Button == MouseButtons.Right)
{
hitTestInfo = dataGridView.HitTest(e.X,e.Y);
//如果列是第一列
if(hitTestInfo.Type == DataGridViewHitTestType.Cell&& hitTestInfo.ColumnIndex == 0)
contextMenuForColumn1.Show(dataGridView,new Point(eX ,eY));
//如果列是第二列
if(hitTestInfo.Type == DataGridViewHitTestType.Cell&& hitTestInfo.ColumnIndex == 1)
contextMenuForColumn2.Show(dataGridView,new Point(eX ,eY));
}
}


I have a datagridview where I show infomation about products. I want to bind a contextmenu when the user selects a cell and then right clicks on that cell. I have another contextmenu and that one is bound to the columns of the datagridview. If a user right clicks on a column the contextmenu shows.

I have tried like this but it does not work. The context menu shows when the user right clicks on a cell, but the contextmenu that is bound to the column header does not work.

   private void GridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            productContextMenu.Show(GridView1, e.Location);
        }

    }

How do I make it so that when the user right clicks on a datagridview shows up?

Many thanx in advance.

EDIT

Thnx for the answers. I solved the problem like this:

    private void GridView1_MouseUp(object sender, MouseEventArgs e)
    {
        DataGridView.HitTestInfo hitTestInfo;
        if (e.Button == MouseButtons.Right)
        {
            hitTestInfo = GridView1.HitTest(e.X, e.Y);
            if (hitTestInfo.Type == DataGridViewHitTestType.Cell)
            {
                productContextMenu.Show(GridView1, e.Location);
            }

        }
    }

Both the contextmenus shows. When I click on the column that context menu shows, and when I click on a cell that contextmenu shows.

解决方案

Try this

 private void dataGridView1_CellMouseDown(object sender, MouseEventArgs e)
{
  if (e.Button == MouseButtons.Right)
  {
        contextMenu.Show(datagridview, e.Location);
  }

} 

or

 private void dataGridView_MouseUp(object sender, MouseEventArgs e)
 {
   // Load context menu on right mouse click
   DataGridView.HitTestInfo hitTestInfo;
   if (e.Button == MouseButtons.Right)
   {
      hitTestInfo = dataGridView.HitTest(e.X, e.Y);
      // If column is first column
      if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 0)
        contextMenuForColumn1.Show(dataGridView, new Point(e.X, e.Y));
    // If column is second column
      if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 1)
        contextMenuForColumn2.Show(dataGridView, new Point(e.X, e.Y));
   }
} 

这篇关于C# - DatagridView和ContextMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆