Get item index from databound DevExpress CheckedLi

2020-04-08 13:57发布

问题:

I am trying to find the index of a particular value from the CheckedListBoxControl. The CheckedListBoxControl has a DataSource, DisplayMember, ValueMember set to a DataTable and two columns receptively. Now I have to set the CheckedState Property to true by finding its index from CheckedListBoxControl by using some value from the ValueMember and then calling the SetItemChecked() method with that index.

I'm not able to find any property or method which returns the index. Please help.

回答1:

If a list box control is bound to a data source, you can iterate throught all listbox items using the the GetItem() method and the ItemCount property:

for(int i = 0; i < checkedListBoxControl.ItemCount; i++) {
    object dataRow = checkedListBoxControl.GetItem(i);
}

To find the index of the specified item you can use the FindItem() method
searching by DisplayText:

string s = "searchString";
int index = checkedListBoxControl.FindItem(startIndex, true, delegate(ListBoxFindItemArgs e) {
   e.IsFound = s.Equals(e.DisplayText);
});

searching by ValueMember:

object value = 100;
int index = checkedListBoxControl.FindItem(startIndex, true, delegate(ListBoxFindItemArgs e) {
   e.IsFound = object.Equals(value, e.ItemValue);
});

Please also take a look at the "How to get checked rows of a data-bound CheckedListBoxControl" article.