0

When I call the context menu the DataGrdiView does not show the menu sub items. But if I call this menu by right clicking on the form, then everything works correctly. The photo shows the same menu. Called on the form and called on the DataGridView. Why can this be so?

enter image description here

    private void ContextMenuStripNeeded(object sender,
       DataGridViewCellContextMenuStripNeededEventArgs e, DataGridView dataGrid)
    {
        if (e.RowIndex > -1 && e.ColumnIndex > -1)
        {

            dataGrid.CurrentCell = dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
            e.ContextMenuStrip = this.contextMenuCellsDataGridView;
            idCurrentTask = dataGrid.Rows[e.RowIndex].Cells[0].Value.ToString();

        }
    }

        private void ShowContextMenuobject(object sender, MouseEventArgs e, DataGridView dataGrid)
    {
        if (e.Button == MouseButtons.Right)
        {
            try
            {
                var hti = dataGrid.HitTest(e.X, e.Y);
                dataGrid.ClearSelection();
                dataGrid.CurrentCell = dataGrid.Rows[hti.RowIndex].Cells[0];
                idCurrentTask = dataGrid.Rows[hti.RowIndex].Cells[0].Value.ToString();

            }
            catch { }
        }
    }

    private void HandlerControls() {
        dgvDayTask.RowTemplate.ContextMenuStrip = contextMenuCellsDataGridView;
        dgvWeekTask.RowTemplate.ContextMenuStrip = contextMenuCellsDataGridView;
        dgvFutureTask.RowTemplate.ContextMenuStrip = contextMenuCellsDataGridView;
    }

GAAAN
  • 399
  • 2
  • 3
  • 14
  • Can you provide the right click events? – Berkay Yaylacı Nov 29 '21 at 13:13
  • private void dgvDayTask_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e) { ContextMenuStripNeeded(sender, e, dgvDayTask); } – GAAAN Nov 29 '21 at 13:21
  • It is difficult to follow what you are trying to do in the posted code. Can you provide some context as to what `ContextMenuStripNeeded` is doing? The same for `ShowContextMenuobject`… Also… Are you 100% sure the grid and form are using the same context menu? I would think that setting the “forms” context menu strip would also apply to ALL the controls on the form or at least to controls that do not take input. I suggest you create an [mre] that demonstrates what you claim. In my small tests I was unable to reproduce what you describe. – JohnG Nov 29 '21 at 19:22

0 Answers0