C#难点解决---两个不同的GridView使用同一个菜单,怎么区别呢

之前就很想做,查了很多资料终于做出来了,之后还可以复用,开心啊
核心代码:获取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();
        }

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-31 17:32:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-31 17:32:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-31 17:32:01       87 阅读
  4. Python语言-面向对象

    2024-03-31 17:32:01       96 阅读

热门阅读

  1. LLM-在CPU环境下如何运行ChatGLM-6B

    2024-03-31 17:32:01       45 阅读
  2. npm常用命令技巧

    2024-03-31 17:32:01       37 阅读
  3. 封装Redis工具类

    2024-03-31 17:32:01       42 阅读
  4. @RequestMapping和@GetMapping的区别

    2024-03-31 17:32:01       37 阅读
  5. 29. 两数相除 —— LeetCode (python)

    2024-03-31 17:32:01       39 阅读
  6. LeetCode刷题记录——day9

    2024-03-31 17:32:01       37 阅读