There is a paragraphCodeAs follows:
Code
Foreach (Campaign Campaign In Dgcampaign. selecteditems)
{
If (Campaign ! = Null && ! Resource. isnullorempty (Campaign ))
{
// Remove from itemsource in the DataGrid
Dgcampaign. itemssourcelist. Remove (Campaign );
SC. Delete (Campaign );
I ++ ;
}
}
When you use the foreach statement to perform the delete operation, the set is modified, and enumeration may not be performed.
The for statement is fine, as shown below:
Code
// This can be done as follows, but because my DataGrid has other operations, you can only use other methods
For ( Int J = 0 ; J < Dgcampaign. selecteditems. Count; j ++ )
{
Campaign Campaign = Dgcampaign. selecteditems [J] As Campaign;
Dgcampaign. itemssourcelist. Remove (Campaign );
SC. Delete (Campaign );
}
// The following method is required
Resset < Campaign > Campaigns = New Resset < Campaign > ();
Foreach (Campaign Campaign In Dgcampaign. selecteditems)
{
If (Campaign ! = Null && ! Resource. isnullorempty (Campaign ))
{
Campaigns. Add (Campaign );
}
}
Foreach (Campaign item In Campaigns)
{
Dgcampaign. itemssourcelist. Remove (item );
SC. Delete (item );
}
Note:
The foreach statement implements system. collections. ienumerable or system. collections. generic. each element in an array or object set of the ienumerable <(of <(T>) interface repeats a set of embedded statements. The foreach statement is used to access the set cyclically to obtain the information you need, but cannot be used to add or remove items in the source set. Otherwise, unpredictable side effects may occur. To add or remove items in the source set, use the for loop.