Mic*_*hop 3 c# datagridview undo master-detail
我有一个主从布局,其中有一部分弹出菜单(“详细信息”)和一个有DataGridView的部分,其中包含行。
当DataGridView中的选定行更改时,弹出菜单状态将更新,而当弹出菜单更改时,DGV的选定行中的状态应更新。
当我更改弹出菜单的值时,除了 DataGridView中的行不会立即更新之外,所有这些工作均有效。我必须选择其他行才能查看我的修改。
我认为这是因为直到选择更改,才提交编辑。
我的问题是:如何使对弹出窗口的更改立即反映在DataGridView中?
我已经尝试过在弹出菜单的SelectionChangeCommitted处理程序中调用EndEdit(),但这没有任何效果。我对允许我创建一个DataGridView的技术感兴趣,该DataGridView的行为就像没有开始的撤消机制一样。理想情况下,该解决方案应该是通用的,并且可以移植到其他项目中。
似乎现有答案与可以很好地配合使用BindingSource
。在我的情况下,where DataTable
被直接用作DataSource
,由于某种原因它们没有起作用。
// Other answers didn't work in my setup...
private DataGridView dgv;
private Form1()
{
var table = new DataTable();
// ... fill the table ...
dgv.DataSource = table;
}
Run Code Online (Sandbox Code Playgroud)
经过一番拉扯后,我无需添加BindingSource
间接就可以正常工作:
// Add this event handler to the DataGridView
private void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dgv.BindingContext[dgv.DataSource].EndCurrentEdit();
}
private Form1()
{
dgv.CellEndEdit += dgv_CellEndEdit;
// ...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17452 次 |
最近记录: |