Facing problem to programmatically check item inside checklistbox

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
this way i bound cheklistbox

if (dsPubs.Tables.Count > 0)
{
if (dsPubs.Tables[0].Rows.Count > 0)
{
// bidning data with combobox from db
((ListBox)checkListClients).DataSource = dsPubs.Tables[0];
((ListBox)checkListClients).DisplayMember = "ClientName";
((ListBox)checkListClients).ValueMember = "ClientCode";
}
}

dsPubs.Tables[0] is datatable.



i was setting check state in this way in loop

foreach (DataRowView item in checkListClients.Items)
{
strClientID = item["ClientCode"].ToString();
//find client code exist in datatable...if exist then check the item
if (dt.AsEnumerable().Any(x => x.Field<string>("ClintCode").ToString().ToUpper() == strClientID.ToUpper()))
{
checkListClients.SetItemChecked(index, true);
}
index++;
}

after binding data when i try to set check item of checklistbox in loop then i got this error


Additional information: List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.


when i try this approach then i am not getting each item name or text

for(int x=0;x<=checkListClients.Items.Count-1;x++)
{
strClientID = checkListClients.Items[x].ToString().ToUpper();
if (dt.AsEnumerable().Any(y => y.Field<string>("ClientName").ToString().ToUpper() == strClientID.ToUpper()))
{
checkListClients.SetItemChecked(x, true);
}
}

see this one strClientID = checkListClients.Items[x].ToString().ToUpper();

in this variable strClientID this value is stored SYSTEM.DATA.DATAROWVIEW and i am getting that instead of actual text of each item.

How can i iterate in checklistbox item and set check item if that text found in my datatable?

please suggest me right approach what i am trying to do. thanks

Continue reading...
 
Back
Top