C# - DatagridView 和 ContextMenu

发布于 2024-11-05 05:03:45 字数 1044 浏览 1 评论 0原文

我有一个 datagridview,在其中显示有关产品的信息。我想在用户选择一个单元格然后右键单击该单元格时绑定一个上下文菜单。我有另一个上下文菜单,该菜单绑定到 datagridview 的列。如果用户右键单击某列,则会显示上下文菜单。

我已经尝试过这样的方法,但它不起作用。当用户右键单击单元格时会显示上下文菜单,但绑定到列标题的上下文菜单不起作用。

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

    }

我该如何做到这一点,以便当用户右键单击 datagridview 时显示出来?

提前非常感谢。

编辑

谢谢你的答案。我这样解决了问题:

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

        }
    }

两个上下文菜单都显示。当我单击上下文菜单显示的列时,以及当我单击上下文菜单显示的单元格时。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望喜 2024-11-12 05:03:45

试试这个

 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)
 {
   // 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));
   }
} 

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));
   }
} 
无可置疑 2024-11-12 05:03:45

对于相对位置的问题,还可以使用这样的办法:

DataGridViewColumn dgvC = new DataGridViewColumn();
DataGridViewRow dgvR = new DataGridViewRow();
dgvC = dgv.Columns[e.ColumnIndex];
dgvR = dgv.Rows[e.RowIndex];
Point p = new Point();
p.X = (dgvC.Width * e.ColumnIndex) + e.X;
p.Y = (dgvR.Height * e.RowIndex) + e.Y;
dgv.ContextMenu.Show(dgv, p);

For the problem with the relative position, you can also use this aproach:

DataGridViewColumn dgvC = new DataGridViewColumn();
DataGridViewRow dgvR = new DataGridViewRow();
dgvC = dgv.Columns[e.ColumnIndex];
dgvR = dgv.Rows[e.RowIndex];
Point p = new Point();
p.X = (dgvC.Width * e.ColumnIndex) + e.X;
p.Y = (dgvR.Height * e.RowIndex) + e.Y;
dgv.ContextMenu.Show(dgv, p);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文