-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMyExtensions.cs
68 lines (55 loc) · 2.29 KB
/
MyExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace EditClipboardContents;
// Get the bound function from the DataGridView for handling the SelectionChanged event, remove it and re-add it after clearing the selection
public static class DataGridViewExtensions
{
public static void ClearSelectionNoEvent(this DataGridView dgv)
{
// 1. Find the static "EVENT_DATAGRIDVIEWSELECTIONCHANGED" field on DataGridView
BindingFlags eventBindingFlags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.GetField;
FieldInfo eventKeyField = typeof(DataGridView).GetField("EVENT_DATAGRIDVIEWSELECTIONCHANGED", eventBindingFlags);
if ( eventKeyField == null )
{
// Field not found (likely .NET Core / .NET 5+). Just clear as fallback.
dgv.ClearSelection();
return;
}
// This is the private object used as the dictionary key in the "Events" EventHandlerList
object eventKey = eventKeyField.GetValue(null);
// 2. Get the EventHandlerList from the inherited Component property
BindingFlags listBindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
PropertyInfo eventsProperty = typeof(Component).GetProperty("Events", listBindingFlags);
if ( eventsProperty == null )
{
dgv.ClearSelection();
return;
}
EventHandlerList eventHandlerList = (EventHandlerList)eventsProperty.GetValue(dgv);
if ( eventHandlerList == null )
{
dgv.ClearSelection();
return;
}
// 3. Grab the existing delegate(s) for the event key
Delegate existingHandlers = eventHandlerList[eventKey];
if ( existingHandlers == null )
{
// No handlers attached, do a simple clear
dgv.ClearSelection();
return;
}
// 4. Temporarily remove them
eventHandlerList.RemoveHandler(eventKey, existingHandlers);
// 5. Clear selection silently
dgv.ClearSelection();
// 6. Re-add the original handlers
eventHandlerList.AddHandler(eventKey, existingHandlers);
}
}