Skip to content

在UcDataGridView控件中,当使用了自定义单元格,并加入按钮后,删除对应的行后,按钮动作会重复响应 #47

Open
@miuser00

Description

@miuser00

在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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions