问题描述
我有一个DataBound 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;
}
}
}
}
否错误,调试执行正在经历 检查过程,但最终我看不到对其进行检查。
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.
在这里已得到很好的解释。
It is well explained here.
所以我们必须找到一种解决方法...我尝试了以下方法,它的工作原理很好...
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...
这篇关于以编程方式检查DataBound CheckListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!