Open
Description
在UcDataGridView控件中,当使用了自定义单元格,并加入按钮后,删除对应的行后,使用ReloadSource函数重新加载控件时,会导致用户控件消息处理函数重复加载,导致多次触发,代码如下
源代码UCDataGridViewRow.cs 193行-201行
foreach (Control item in this.panCells.Controls)
{
if (item is IDataGridViewCustomCell)
{
IDataGridViewCustomCell cell = item as IDataGridViewCustomCell;
//此处消息处理链未被清空,导致重复加载
cell.RowCustomEvent += cell_RowCustomEvent;
cell.SetBindSource(DataSource);
}
}
改为如下代码,貌似解决了这个问题
// 获取事件信息
EventInfo eventInfo = cell.GetType().GetEvent("RowCustomEvent");
// 如果事件存在
if (eventInfo != null)
{
// 获取字段,该字段包含了已订阅的委托
FieldInfo fieldInfo = cell.GetType().GetField("RowCustomEvent", BindingFlags.Instance | BindingFlags.NonPublic);
if (fieldInfo != null)
{
// 获取已订阅的委托
MulticastDelegate multicastDelegate = fieldInfo.GetValue(cell) as MulticastDelegate;
if (multicastDelegate != null)
{
// 逐个取消订阅已订阅的委托
foreach (Delegate handler in multicastDelegate.GetInvocationList())
{
eventInfo.RemoveEventHandler(cell, handler);
}
}
}
}
cell.RowCustomEvent += cell_RowCustomEvent;
cell.SetBindSource(DataSource);
Metadata
Metadata
Assignees
Labels
No labels