Click here to Skip to main content
13,357,219 members (78,476 online)
Rate this:
 
Please Sign up or sign in to vote.
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,

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 2-Oct-12 20:47pm
Updated 2-Oct-12 21:44pm
DaveAuld194.1K
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.


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++;

    }
}
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.180111.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100