question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked TZacks-2728 answered

C# Winform: How to show context menu when user will click on datagridview button column

On button click i show context menu just below the button this way.

 private void button1_Click(object sender, EventArgs e)
             {
                 Point screenPoint = button1.PointToScreen(new Point(button1.Left, button1.Bottom));
                 if (screenPoint.Y + contextMenuStrip1.Size.Height > Screen.PrimaryScreen.WorkingArea.Height)
                 {
                     contextMenuStrip1.Show(button1, new Point(0, -contextMenuStrip1.Size.Height));
                 }
                 else
                 {
                     contextMenuStrip1.Show(button1, new Point(0, button1.Height));
                 } 
             }


Now i want to show context menu when user click on button column. i guess this way i can achieve.

 private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
 {
     if (e.Button == MouseButtons.Right)
     {
         ContextMenu m = new ContextMenu();
         m.MenuItems.Add(new MenuItem("Cut"));
         m.MenuItems.Add(new MenuItem("Copy"));
         m.MenuItems.Add(new MenuItem("Paste"));
    
         int currentMouseOverRow = dataGridView1.HitTest(e.X,e.Y).RowIndex;
    
         if (currentMouseOverRow >= 0)
         {
             m.MenuItems.Add(new MenuItem(string.Format("Do something to row {0}", currentMouseOverRow.ToString())));
         }
    
         m.Show(dataGridView1, new Point(e.X, e.Y));
    
     }
 }

but the problem is here i am mention x & y this way m.Show(dataGridView1, new Point(e.X, e.Y));

but to set context menu's position dynamically when click on button column. suppose when i will click on last button column then menu will be show top of that button column and menu will be show below when there will be good space.

Here is a sample code by which i was trying to achieve this.

 private void Form1_Load(object sender, EventArgs e)
 {
     DataTable dt = new DataTable();
     dt.Columns.Add("ID", typeof(int));
     dt.Columns.Add("Name", typeof(string));
     dt.Columns.Add("Salary", typeof(int));
    
     DataRow dr = null;
     dr = dt.NewRow();
     dr[0] = 1;
     dr[1] = "Joydip";
     dr[2] = 5200;
     dt.Rows.Add(dr);
    
     dr = dt.NewRow();
     dr[0] = 2;
     dr[1] = "Salim";
     dr[2] = 3200;
     dt.Rows.Add(dr);
    
     dr = dt.NewRow();
     dr[0] = 3;
     dr[1] = "Sukumar";
     dr[2] = 6000;
     dt.Rows.Add(dr);
    
     dgList.AutoGenerateColumns = false;
     dgList.DataSource = dt;
     dgList.Columns[0].DataPropertyName = "ID";
     dgList.Columns[1].DataPropertyName = "Name";
     dgList.Columns[2].DataPropertyName = "Salary";
    
 }
    
 private void dgList_MouseClick(object sender, MouseEventArgs e)
 {
     int currentMouseOverRow = dgList.HitTest(e.X, e.Y).RowIndex;
     if (currentMouseOverRow >= 0)
         contextMenuStrip1.Show(dgList, new Point(e.X, e.Y));
            
 }

I tried also this code but no luck.

 private void dgList_MouseClick(object sender, MouseEventArgs e)
 {
     DataGridViewCell currentCell = (sender as DataGridView).CurrentCell;
     if (currentCell != null)
     {
         ContextMenuStrip cms = currentCell.ContextMenuStrip;
         Rectangle r = currentCell.DataGridView.GetCellDisplayRectangle(currentCell.ColumnIndex, currentCell.RowIndex, false);
         Point p = new Point(r.X + r.Width, r.Y + r.Height);
         contextMenuStrip1.Show(currentCell.DataGridView, p);
     }
 }

please share some idea to position menu dynamically.

Thanks



dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TZacks-2728 avatar image
0 Votes"
TZacks-2728 answered

Issue fixed. here the working code. Thanks

 private void dgList_MouseClick(object sender, MouseEventArgs e)
 {
     DataGridViewCell currentCell = (sender as DataGridView).CurrentCell;
     if (currentCell != null && currentCell.ColumnIndex==3)
     {
         ContextMenuStrip cms = currentCell.ContextMenuStrip;
         Rectangle r = currentCell.DataGridView.GetCellDisplayRectangle(currentCell.ColumnIndex, currentCell.RowIndex, false);
         Point p = new Point(r.X, r.Y + r.Height);
         contextMenuStrip1.Show(currentCell.DataGridView, p);
     }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.