当我右键单击网格行时,它会显示分配给它的上下文菜单条几秒钟,然后将其替换为表单的上下文菜单。如果点击在网格上,我怎样才能确保网格的上下文菜单条保持可见。
代码:
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);
}
}
答案 0 :(得分: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;
}