之前就很想做,查了很多资料终于做出来了,之后还可以复用,开心啊
核心代码:获取gridView和对应标示isOptions
private (GridView gridView, bool isOptions) GetGridViewAndOptions(object sender)
{
if (sender is ToolStripItem menuItem)
{
var strip = menuItem.Owner as ContextMenuStrip;
if (strip?.SourceControl is GridControl gridControl)
{
var sourceGridView = gridControl.FocusedView as GridView;
if (sourceGridView != null)
{
bool isOptions = sourceGridView == gvProductInput;
return (sourceGridView, isOptions);
}
}
}
XtraMessageBox.Show("操作失败,无法获取上下文菜单信息!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return (null, false);
}
代码里使用:
private void MenuItemUpdate_Click(object sender, EventArgs e)
{
var strFactory = gvProject.GetFocusedRowCellValue("FactoryNo")?.ToString().Trim();
var strProject = gvProject.GetFocusedRowCellValue("ProjectNo")?.ToString().Trim();
var IsOptions = false; var IsAdd = false; string strId = "";
var (gridView, isOptions) = GetGridViewAndOptions(sender);
if (gridView == null) return; // 如果 gridView 为 null,则返回(辅助方法已显示错误消息)
strId = gridView.GetFocusedRowCellValue("Id").ToString().Trim();
ProductPartsDefectEdit frm = new ProductPartsDefectEdit(strFactory, strProject, IsOptions, IsAdd , strId);
frm.ShowDialog();
LoadLsit();
}