Dev Express的CheckedListBoxControl全选及遍历方法

CheckedListBoxControl实现全选与取消的功能:

//设置全选功能,适合CheckedListBoxControl与CheckedListBox控件 
//添加一个名为Select_All的CheckedListBoxControl控件,由其控制CheckedListBox是全选还是全不选。

private void Select_All_CheckedChanged(object sender, EventArgs e)
  {
            if (Select_All.Checked)
            {
                for (int j = 0; j < checkedLBC_JobStatus.Items.Count; j++)
                    checkedLBC_JobStatus.SetItemChecked(j, true);
            }
            else
            {
                for (int j = 0; j < checkedLBC_JobStatus.Items.Count; j++)
                    checkedLBC_JobStatus.SetItemChecked(j, false);
            }
}

CheckBoxListControl实现遍历得到每个选项的值的功能:

对于此功能,今天作了许多尝试,最后找到了解决方法,关于各种取值的结果尝试心得,说明如下:

 1.尝试方法一

strCollected = checkedLBC_JobStatus.GetItemValue(checkedLBC_JobStatus.Items[i]);

此方法适应于WinForm自带的CheckedListBox控件,取的控件文本对应的值而不是文本内容,而不适用于DevExpress的CheckedListBoxControl控件

2.尝试方法二

strCollected = checkedLBC_JobStatus.GetItemText(checkedLBC_JobStatus.Items[i]);

此方法适用于WinForm自带的CheckedListBox控件,取的是控件文对应的内容,而不适用于DevExpress的CheckedListBoxControl控件

3.尝试方法三

strCollected = checkedLBC_JobStatus.SelectedValue.ToString();

此方法可以取值,但只能取到选中的一项,而不能遍历所有选中项


以上说明,希望能够帮助那些苦寻方法的朋友,正确的遍历方法,代码如下,得解请给赞,以慰藉辛苦付出,谢谢!

//遍历选中的值 ,并将选中的项组合成为一个字符串。

 //strCollected = checkedLBC_JobStatus.GetItemText(checkedLBC_JobStatus.Items[i]);

//strCollected = checkedLBC_JobStatus.GetItemValue(checkedLBC_JobStatus.Items[i]);

                    string strCollected = string.Empty;
                    for (int i = 0; i < checkedLBC_JobStatus.Items.Count; i++)
                    {
                        //checkedLBC_JobStatus.SetItemChecked(i, true);

                        if (checkedLBC_JobStatus.GetItemChecked(i))
                        {
                          if (strCollected == string.Empty)
                          {
                              //strCollected = checkedLBC_JobStatus.SelectedValue.ToString();
                              strCollected = this.checkedLBC_JobStatus.GetItemValue(i).ToString();
                          }
                          else
                          {
                              //strCollected = strCollected + "," + checkedLBC_JobStatus.SelectedValue.ToString();
                              strCollected = strCollected + "," + checkedLBC_JobStatus.GetItemValue(i).ToString();
                           }
                         }
                    }

                    string JobStatus = string.Format(" and uvw_ppJobQuery.JobStatusID in {0}", "(" + strCollected.ToString() + ")");


你可能感兴趣的:(C#,学习心得)