Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - DatagridView and ContextMenu

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.

like image 885
Erika Avatar asked May 04 '11 14:05

Erika


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


1 Answers

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));
   }
} 
like image 184
Developer Avatar answered Oct 24 '22 07:10

Developer