0

我有以下代码来选择 CheckListBox 中的下一个列表项或第一个列表项:

        var calculateRate = cancellation.CancellationCalculateRates;
        foreach (ListItem item in chkListRate.Items)
        {
            chkListRate.DataBind(); >>> Error Collection was modified; enumeration operation may not execute.
            var cancel = ConverterHelper.To<int>(item.Value);
            item.Selected = calculateRate.Any(i => i.CancellationId.Equals(cancel));
        }

如何在 CheckListbox 上选择值后进行 DataBind?

4

1 回答 1

0

As the error message says, you cannot modify a collection during enumeration(foreach). So deleting and inserting is not allowed. CheckBoxList.DataBind is modifying it therefore the error.

But it's not clear what you are actually trying to do. You are databinding the list on every item which seems to be pointless, isn't it? So my sugestion is to remove the chkListRate.DataBind(); from the loop:

foreach (ListItem item in chkListRate.Items)
{
    var cancel = ConverterHelper.To<int>(item.Value);
    item.Selected = calculateRate.Any(i => i.CancellationId.Equals(cancel));
}
于 2014-02-17T08:50:23.860 回答