如何通信DataGridView行索引到演示者

本文关键字:索引 DataGridView 何通信 通信 | 更新日期: 2023-09-27 18:10:37

我在表格上有一个DataGridView,我在Presenter中绑定了一个BindingListGrid

public AttendancePresenter( IAttendance model, IAttendanceView view, IDataService dataService, IMessageService messageService ) : base(messageService)
{
    _BindingAttendanceList = new BindingList<IAttendance>();
    _View.AttendanceGrid = _BindingAttendanceList;
}

现在我想从网格中删除选中的行,当我按下delete按钮时(这个更改应该在数据库中更新)。我的问题是我应该如何通知Presenter,我想删除这个特定的行/项在网格中?如果演示者知道它可以从BindingList中找到被删除的项目,并从数据库中删除相同的记录。(ID字段AttendanceID字段可用于此)

注意:我的View不知道PresenterView只在用户操作时触发事件。

private void btnDelete_Click(object sender, EventArgs e)
{
    OnDeleteAttendance(sender, e);
}

编辑:我的网格有几个列,如AttendanceID, EmployeeID, Name, InDateTime, OutDateTime等,我在视图中使用一个公共属性来设置gridfrompresenter

public BindingList<IAttendance> AttendanceGrid
{
    Set { dgvAttendance.DataSource = Value; };
}

如何通信DataGridView行索引到演示者

如果我理解正确的话,您本质上问的是如何将您想要从视图中删除的项目的ID传递给演示器

有两种典型的方法:

  1. View接口中创建一个属性,用于获取要删除项目的ID

    public int IdToDelete
    {
        get
        {
            // logic to get the id of the item you want to delete
        }
    }
    

    通过这种方式,您可以在演示器中访问您想要删除的项目的ID。

  2. 另一种方法是扩展EventArgs类并添加另一个属性来存储项目的ID,但我认为这对于传递单个值来说是多余的。