I have a DataBound “CheckedListBox”, I need to check some items on it. I tried with following code…
if (!string.IsNullOrEmpty(search.Languages))
{
string[] langs = search.Languages.Split(',');
for (int i = 0; i < (langs.Length - 1); i++)
{
for (int j = 0; j < clbLang.Items.Count; j++)
{
string lng = clbLang.Items[j] as string;
if (lng.Trim() == langs[i])
{
clbLang.SetItemChecked(j, true);
break;
}
}
}
}
No errors, debuged execution is going through “checking” process, but finally I cannot see anything checked on it.
Then I have added a button and added following code to it. (upon click check all the items)
private void button9_Click(object sender, EventArgs e)
{
for (int i = 0; i < clbLang.Items.Count; i++)
{
clbLang.SetItemChecked(i, true);
}
}
It is “checking” all the items, Please tell me if anyone can see a issue here…?
Finally found out, it is a Bug introduced by MS.
It is well explained here.
CheckedListBox SetItemChecked method not working
So we have to find a workaround… I tried follwing way, it is working nice…
At the place where I was calling checking of items I have added following… I am adding what I need to check in Tag of the control.
Then, following code in that control’s “VisibleChanged()” event.
This works well with me, hope it will benefit you…