You need to enable JavaScript to run this app. C# MVP架构下实现DataGridView选中行绑定到文本框与复选框及报错解决
优惠活动
大模型
产品
解决方案
定价
更多

C# MVP架构下实现DataGridView选中行绑定到文本框与复选框及报错解决

MVP架构下的DataGridView选中行同步逻辑实现方案

报错根因拆解

三个编译报错全部源于Presenter层和视图层的边界耦合错误,没有遵循MVP分层的依赖规则:

  • 当前上下文中不存在名称“e”:将视图层的事件处理逻辑直接移入Presenter时,没有把CellClick事件携带的参数e作为方法入参传入,Presenter作为独立的类,本身不持有视图控件的事件上下文,自然无法找到该参数
  • “object”类型未包含“Rows”的定义:向Presenter传递控件引用时,没有做明确的类型声明与转换,直接将DataGridView实例作为object类型传入,Presenter无法解析object上的控件成员
  • 无法将“string”类型隐式转换为“int”类型:错将单元格中存储的字符串值(比如文本框内的输入、单元格内的字符串格式ID)作为DataGridView.Rows方法需要的int型行索引传入,没有做类型校验与转换

落地实现步骤

1. 定义视图抽象接口

这是MVP解耦的核心,Presenter不直接依赖具体的WinForm控件,只依赖抽象接口,所有视图层的交互能力都在接口中声明:

public interface IDataGridViewView
{
    // 声明DataGridView的CellClick事件,将视图层的事件向外抛出
    event EventHandler<DataGridViewCellEventArgs> GridCellClick;
    // 声明需要同步到视图控件的属性,由Presenter直接赋值
    string ShowBox1Value { set; }
    string ShowBox2Value { set; }
    string ShowBox3Value { set; }
    bool ShowCheckValue { set; }
    // 封装视图层的操作能力,禁止将控件实例泄露给Presenter
    int GetValidRowIndex(DataGridViewCellEventArgs e);
    string GetCellContent(int rowIndex, int cellIndex);
}

2. 视图层(WinForm窗体)实现接口

所有控件的实际操作全部封装在窗体内部,不把任何具体控件实例暴露给Presenter:

public partial class MainForm : Form, IDataGridViewView
{
    public MainForm()
    {
        InitializeComponent();
        // 将DataGridView原生的CellClick事件绑定到接口声明的对外事件上
        dataGridView1.CellClick += (sender, e) => GridCellClick?.Invoke(sender, e);
    }

    public event EventHandler<DataGridViewCellEventArgs> GridCellClick;

    // 接口属性的实际实现,直接操作窗体上的控件
    public string ShowBox1Value { set => textBox1.Text = value; }
    public string ShowBox2Value { set => textBox2.Text = value; }
    public string ShowBox3Value { set => textBox3.Text = value; }
    public bool ShowCheckValue { set => checkBox1.Checked = value; }

    public int GetValidRowIndex(DataGridViewCellEventArgs e)
    {
        // 过滤点击表头时的无效行索引
        if (e.RowIndex < 0) return -1;
        return e.RowIndex;
    }

    public string GetCellContent(int rowIndex, int cellIndex)
    {
        // 做空值兼容,避免空引用报错
        return dataGridView1.Rows[rowIndex].Cells[cellIndex].Value?.ToString() ?? string.Empty;
    }
}

3. Presenter层实现业务逻辑

Presenter只依赖视图接口,不直接接触任何WinForm控件,从根本上避免之前的编译错误:

public class GridDataPresenter
{
    private readonly IDataGridViewView _view;

    public GridDataPresenter(IDataGridViewView view)
    {
        _view = view;
        // 订阅视图抛出的CellClick事件
        _view.GridCellClick += OnGridCellClicked;
    }

    private void OnGridCellClicked(object sender, DataGridViewCellEventArgs e)
    {
        // 事件参数e会随订阅的事件自动传入Presenter的方法,不存在找不到的问题
        int rowIndex = _view.GetValidRowIndex(e);
        if (rowIndex < 0) return;

        // 通过视图接口获取单元格数据,同步到视图控件
        _view.ShowBox1Value = _view.GetCellContent(rowIndex, 0);
        _view.ShowBox2Value = _view.GetCellContent(rowIndex, 1);
        _view.ShowBox3Value = _view.GetCellContent(rowIndex, 2);
        _view.ShowCheckValue = Convert.ToBoolean(_view.GetCellContent(rowIndex, 4));
    }
}

报错对应修复点

  • 针对e不存在:通过事件订阅的方式,将CellClick的事件参数自动传入Presenter的处理方法,不需要在Presenter内部硬编码寻找上下文参数
  • 针对object类型无Rows定义:所有DataGridView的控件操作全部封装在视图层,Presenter不需要持有控件实例,也就不存在类型转换错误
  • 针对string转int失败:行索引全部通过视图层的GetValidRowIndex方法获取,该方法直接返回int类型的有效索引,不会误把字符串类型的单元格值当索引传入

内容的提问来源于stack exchange,提问作者Salem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.13 16:45:06