【问题标题】:DataGridView_CellEndEdit event (C#)DataGridView_CellEndEdit 事件 (C#)
【发布时间】:2015-09-17 05:11:48
【问题描述】:

我正在编辑 DataGridViews 中的值并因此更新数据库中的新值。我正在使用 CellEndEdit() 事件来存储新值。但是,由于我需要编辑多个 DataGridViews,我可以只使用一次“CellEndEdit”事件,使其对所有 DataGrids 通用,而不是为每个 DataGrids 初始化这个事件吗?

我正在像这样使用 eventHandler:

classViewDataGrid1.CellEndEdit += new DataGridViewCellEventHandler(classViewDataGrid1_CellEndEdit);

private void classViewDataGrid1_CellEndEdit(object sender,DataGridViewCellEventArgs e)            
{
   string newValue = classViewDataGrid1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
   string attribValue = classViewDataGrid1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString();
   EditClassProperties.editProp(newValue, attribValue, EditdDbProp.selNode); //Calling the function that updates database.
}

【问题讨论】:

    标签: c# datagridview datagrid


    【解决方案1】:

    这正是sender 部分在EventHandler 中的用途。我建议给this documentation 好好读一读。

    如果您希望其他 DataGridView 使用完全相同的事件处理程序,您可以将代码更改为此。

    classViewDataGrid1.CellEndEdit += new DataGridViewCellEventHandler(classViewDataGrid1_CellEndEdit);
    
    private void classViewDataGrid1_CellEndEdit(object sender,DataGridViewCellEventArgs e)            
    {
       var dgv = (DataGridView) sender;
       string newValue = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
       string attribValue = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString();
       EditClassProperties.editProp(newValue, attribValue, EditdDbProp.selNode); //Calling the function that updates database.
    }
    

    【讨论】:

    • 这是一个选项。但我想要的是.. 我的应用程序中有 7 个 dataGridViews。因此,我想使用单个“CellEndEdit”事件相应地编辑所有 dataGridViews 的值,而不是使用每个 dataGridView 的 7 个不同事件。你的建议在这方面没有帮助。如果你能建议我一些其他的。无论如何感谢您的帮助。
    • 您可以将此 EventHandler 传递给所有其他 DataGridView,它将从相应的 DataGridView 中获取值。通过使用sender,它知道哪个 DataGridView 正在调用事件。对我来说,这似乎正是你想要的,如果我错了,请为第二个 DataGridView 添加 EventHandler 的代码,以帮助我理解你想要实现的目标。
    猜你喜欢
    • 1970-01-01
    • 2012-12-21
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多