【问题标题】:Devexpress datagrid and form context menuDevexpress 数据网格和表单上下文菜单
【发布时间】:2013-06-10 09:23:03
【问题描述】:

当我右键单击网格行时,它会显示分配给它的上下文菜单条几秒钟,然后将其替换为表单的上下文菜单。如果单击网格,我如何确保网格的上下文菜单条保持可见。

代码:

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

【问题讨论】:

  • WPF?表格? ASP.Net?
  • 它的 winform 抱歉忘了提
  • 无法重现您的问题。是您自己的 ContextMenuStrip 在 Form 的即将到来的,还是来自 LayoutControl 或 sth 的标准 ContextMenu?
  • 我自己的 ContextMenuStrip 分配给表单
  • @RahulVasantraoKamble:将以下行添加到 Form_Load() 方法中应该可以解决问题:gridControl1.ContextMenuStrip = ctsGrid;

标签: c# winforms devexpress contextmenustrip


【解决方案1】:

这是可能的解决方案:

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

【讨论】:

  • 如果我想继续将 ctsGrid 分配给仅查看而不是网格控制...,您能提出任何建议吗?
  • @RahulVasantraoKamble 视图不是控件,因此您不能将 ContextMenuStrip 分配给 gridView。无论如何,我建议您覆盖视图的嵌入式菜单,而不是使用带有网格控件的 ContextMenuStrip。要覆盖视图的上下文菜单,请使用 PopupMenuShowing 事件。要了解更多信息,请查看documentation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-25
  • 2013-12-03
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多