Question

When i right click on grid row it shows context menu strip assigned to it for few second and then replace it with form's context menu. How can i make sure that grid's context menu strip remain visible if click is on grid.

Code:

 private void Form1_Load(object sender, EventArgs e)
    {
        Matches();
        DataTable dt = new DataTable();
        dt.Columns.Add("Test", typeof(string));
        dt.Rows.Add("A");
        dt.Rows.Add("A");
        dt.Rows.Add("A");
        dt.Rows.Add("A");
        dt.Rows.Add("A");
        dt.Rows.Add("A");
        ContextMenuStrip ctsForm = new ContextMenuStrip();
        ctsForm.Items.Add("Form");
       ctsForm.Opening+=ctsForm_Opening;
        ctsGrid.Items.Add("Grid");
        gridControl1.DataSource = dt;

        gridView1.PopupMenuShowing+=gridView1_PopupMenuShowing;
        this.ContextMenuStrip = ctsForm;

    }

    private void ctsForm_Opening(object sender, CancelEventArgs e)
    {


    }

    private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
    {
        GridView view = (GridView)sender;
        GridHitInfo hitInfo = view.CalcHitInfo(e.Point);
        if (hitInfo.InRow)
        {
            view.FocusedRowHandle = hitInfo.RowHandle;
            ctsGrid.Show(view.GridControl, e.Point);
        }
    }
Was it helpful?

Solution

Here is the possible solution:

void Form1_Load(object sender, EventArgs e) {
    DataTable dt = new DataTable();
    dt.Columns.Add("Test", typeof(string));
    dt.Rows.Add("A");
    dt.Rows.Add("A");
    dt.Rows.Add("A");
    dt.Rows.Add("A");
    dt.Rows.Add("A");
    dt.Rows.Add("A");
    gridControl1.DataSource = dt;

    ContextMenuStrip ctsForm = new ContextMenuStrip();
    ctsForm.Items.Add("Form");
    ctsForm.Opening += ctsForm_Opening;
    this.ContextMenuStrip = ctsForm;

    ContextMenuStrip ctsGrid = new ContextMenuStrip();
    ctsGrid.Items.Add("Grid Row!");
    ctsGrid.Opening += ctsGrid_Opening;
    gridControl1.ContextMenuStrip = ctsGrid;

    // gridView1.PopupMenuShowing removed at all
}
void ctsGrid_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
    e.Cancel = !IsPointInGridRow(gridView1, gridControl1.PointToClient(Control.MousePosition));
}
void ctsForm_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
   // some code
}
static bool IsPointInGridRow(GridView view, Point pt) {
    return view.CalcHitInfo(pt).InRow;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top