编程方式检查数据绑定CheckListBox [英] Programatically Checking DataBound CheckListBox
问题描述
我有一个数据绑定的CheckedListBox,我需要检查它的一些项目。我试着用下面的代码...
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;
}
}
}
}
没有错误,debuged执行正在经历的检查的的过程,但最后我看不到任何东西就可以了检查。
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...?
推荐答案
终于找到了,这是一个错误按MS出台。
Finally found out, it is a Bug introduced by MS.
大家都在这里解释。
这个问题是很容易繁殖。只是隐藏和显示数据绑定
的CheckedListBox,你会发现
之前如何检查的项目获得
选中。
The issue is easy to reproduce. Just hide and show a databound CheckedListBox and you will notice how the previously checked items get unchecked.
的CheckedListBox SetItemChecked方法不工作
因此,我们必须找到一个解决办法......我试过follwing方式,它是工作不错...
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.
if (!string.IsNullOrEmpty(search.Languages))
{
clbLang.Tag = search.Languages;
}
然后,按照该控件的VisibleChanged()事件代码。
Then, following code in that control's "VisibleChanged()" event.
private void clbLang_VisibleChanged(object sender, EventArgs e)
{
string lngs = clbLang.Tag as string;
if (!string.IsNullOrEmpty(lngs))
{
string[] langs = lngs.Split(',');
foreach (string lang in langs)
{
int j = 0;
foreach (DataRowView row in clbLang.Items)
{
if (row != null)
{
string lng = row[1] as string;
if (lng.Trim() == lang)
{
clbLang.SetItemChecked(j, true);
break;
}
}
j++;
}
}
}
}
这和我一起工作得很好,希望这将有利于你...
This works well with me, hope it will benefit you...
这篇关于编程方式检查数据绑定CheckListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!