You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
When a CheckedListBox is bound to a List<T>, CheckedListBox.RefreshItems() raises an IndexOutOfRangeException during the DataSource assignment. #15377
Did it work in any of the earlier releases of .NET Core or .NET 5+?
No response
Issue description
When a CheckedListBox is bound to a List<T>, CheckedListBox.RefreshItems() raises an IndexOutOfRangeException during the DataSource assignment. The exception is caught internally, so the application continues and the items are populated. However, if DisplayMember was assigned before DataSource, the setter's catch path clears DisplayMember, and the list displays each item's ToString() value instead of the selected property.
Observe the first chance exception during the DataSource assignment. Repeating the binding with a list of a different item count also triggers it.
Expected behavior
The data source should be bound without an exception, and the assigned DisplayMember should remain in effect.
Actual behavior
CheckedListBox.RefreshItems() indexes the saved check state using the newly populated item count, although the saved state was sized using the previous Items.Count. This raises an IndexOutOfRangeException. The exception is swallowed, the application continues, and the items are populated, but DisplayMember is reset to an empty value when it was set before DataSource. Setting DisplayMember after DataSource displays the selected property correctly.
One possible fix is to limit the state restoration loop to the smaller of the saved state length and the current item count.
Hi, i was the original reporter of the issue. I have one question, should one actually restore the selected check in all cases?
Let's say you have a form with a button that goes something like this:
public Form1()
{
InitializeComponent();
changeItemsButton.Click+= (a, b) => {
var tl = new List<TextList>();
tl.Add(new TextList() { ID =4, Name ="Test4" });
tl.Add(new TextList() { ID =5, Name ="Test5" });
tl.Add(new TextList() { ID =6, Name ="Test6" });
objectList.DataSource= tl;
};
var tl = new List<TextList>();
tl.Add(new TextList() { ID =1, Name ="Test" });
tl.Add(new TextList() { ID =2, Name ="Test2" });
tl.Add(new TextList() { ID =3, Name ="Test3" });
objectList.DataSource= tl;
objectList.DisplayMember="Name";
}
In this case, the items added are nothing like the previous items. Should one really restore their checked state, just based on the previously selected index?
Thanks for asking, @siggemannen. CheckedListBox has preserved check states by index in RefreshItems since the first open-source commit of System.Windows.Forms in 2018. PR #15382 didn't add that behavior; it only limits restoration to indexes present in both lists, preventing the out-of-range exception and the DisplayMember reset when rebinding to a longer list.
Starting a new data source with no checks would change existing behavior, and some applications may rely on checks staying selected when they rebind the same or refreshed data. That falls outside this fix. If you'd prefer new sources to start unchecked, would you consider opening a separate suggestion issue? As a workaround, you can clear the checks after assigning the new data source:
Activity
Olina-Zhang commentedon Nov 12, 2026
Verified it in the latest .NET 12 build: 12.0.100-alpha.1.26560.109 + dlls built from Winforms repo, this issue was fixed:
GH15377_Verify.mp4
siggemannen commentedon Nov 12, 2026
Hi, i was the original reporter of the issue. I have one question, should one actually restore the selected check in all cases?
Let's say you have a form with a button that goes something like this:
public Form1() { InitializeComponent(); changeItemsButton.Click += (a, b) => { var tl = new List<TextList>(); tl.Add(new TextList() { ID = 4, Name = "Test4" }); tl.Add(new TextList() { ID = 5, Name = "Test5" }); tl.Add(new TextList() { ID = 6, Name = "Test6" }); objectList.DataSource = tl; }; var tl = new List<TextList>(); tl.Add(new TextList() { ID = 1, Name = "Test" }); tl.Add(new TextList() { ID = 2, Name = "Test2" }); tl.Add(new TextList() { ID = 3, Name = "Test3" }); objectList.DataSource = tl; objectList.DisplayMember = "Name"; }In this case, the items added are nothing like the previous items. Should one really restore their checked state, just based on the previously selected index?
ricardobossan commentedon Nov 13, 2026
Thanks for asking, @siggemannen.
CheckedListBoxhas preserved check states by index inRefreshItemssince the first open-source commit ofSystem.Windows.Formsin 2018. PR #15382 didn't add that behavior; it only limits restoration to indexes present in both lists, preventing the out-of-range exception and theDisplayMemberreset when rebinding to a longer list.Starting a new data source with no checks would change existing behavior, and some applications may rely on checks staying selected when they rebind the same or refreshed data. That falls outside this fix. If you'd prefer new sources to start unchecked, would you consider opening a separate suggestion issue? As a workaround, you can clear the checks after assigning the new data source:
@LeafShi1, could you confirm or add anything I've missed?
LeafShi1 commentedon Nov 16, 2026
Thanks, Ricardo. You’re right, the current fix only addresses the IndexOutOfRangeException and does not alter the original behavior.