Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
miuser00 opened this issue Sep 20, 2023 · 3 comments

Comments

@miuser00
Copy link

在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);
@stevenzhao719
Copy link

确实有这个BUG,也发现了

@kwwwvagaa
Copy link
Owner

kwwwvagaa commented Sep 24, 2023 via email

@stevenzhao719
Copy link

cell.RowCustomEvent -= cell_RowCustomEvent;
cell.RowCustomEvent += cell_RowCustomEvent; 最简单的方法,先减一次再加一次,这样就不重复了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants