Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone,
I am having a problem with checkedlistbox, can anyone help me, or give me some idea what should i do to fix this thing..

I have a Checkedlistbox with jobs in it.
I want to check the jobs assigned to employees from dataset and check only those jobs true..
The problem is when it checks the 1st Job true,
it comes out of foreach loop and throws exception.
"List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change."
This is the code i have written,

C#
string sSQL="select JOBNO from RentalVehicle";
dataset ds = munshi.executeDS(sSQL,"Jobs").Tables["Jobs"];

                    foreach(DataRow Erow in ds.Rows)	
		    {
                    int CLB_RowNo = 0;
                    string[] JNos = Erow["JOBNO"].ToString().Split(', ');	//Jobdetail(2010-0191, HWS-01, ISB-6,KAD2-2, MAINT-01, PMO-2011-0052)

                    string SelectedJob = "", AJob = "";
                    foreach (Object obj in CLB_JobNo.Items)	// Looping CLB_JobNo(CheckedListBox)
                    {
                        int i = 0;
                        SelectedJob = obj.ToString();
                        foreach (string part in JNos)
                        {
                            AJob = JNos[i].ToString();
                            if (SelectedJob == AJob)
                            {
                                CLB_JobNo.SetItemChecked(CLB_RowNo, true);
                            }
                            i++;
                        }
                        CLB_RowNo++;
                    }


Thanks for every reply..
Posted
Updated 2-Oct-12 20:44pm
v3

1 solution

Hi,

Your inner loop traversal is incorrect.

In innner loop you have used foreach but accessing item with i(variable);

Modify the inner loop code as below.


C#
string sSQL="select JOBNO from RentalVehicle";
dataset ds = munshi.executeDS(sSQL,"Jobs").Tables["Jobs"];

foreach(DataRow Erow in ds.Rows)
{
   int CLB_RowNo = 0;
   string[] JNos = Erow["JOBNO"].ToString().Split(', ');
   //Jobdetail(2010-0191, HWS-01, ISB-6,KAD2-2, MAINT-01, PMO-2011-0052)

   string SelectedJob = "", AJob = "";
   foreach (Object obj in CLB_JobNo.Items) // Looping CLB_JobNo(CheckedListBox)
   {
        SelectedJob = obj.ToString();
        foreach (string part in JNos)
        {
            AJob = part.ToString();
            if (SelectedJob == AJob)
            {
                  CLB_JobNo.SetItemChecked(CLB_RowNo, true);
            }
         }
         CLB_RowNo++;

    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900