enlist contents from CheckedItems collection of CheckedListBox

A

Aamir

What is the straightforward way to get the checked contents of a
CheckedListBox.
I have a method that is supposed to return the csv string value out of
CheckedItemCollection. But I don't kow how to access the actual values
when iterating through the collection (value can be either
DisplayMember or ValueMember since this CheckedListBox is populated
from a DataSet table)

In the caller method I call the methid supplying it the CheckedItems
property of the list box:

string strCitiesList = ListToCSV(lstCities.CheckedItems);

//The method definition

private string ListToCSV(CheckedListBox.CheckedItemCollection lst)
{
foreach (Object obj in lst)
{
MessageBox.Show(obj.ToString());
}
}
right now I am only displaying the value, but it shows
"System.Data.DataRowView"
instead of the selected values.

Instead of the "Object" type in the foreach loop I tried to use
"CheckedItemCollection.Item" type but it shows error on compile time.


Any help will be appreciated.
 
A

Aamir

Answering myself.
I change the type of indexer from Bject to DataRowView and
then used the squarebrackets enclosing the name of the
tabel column I needed to display:

//***************************
private string ListToCSV
(CheckedListBox.CheckedItemCollection lst)
{
foreach (DataRowView obj in lst)
{
MessageBox.Show(obj["ID"]);
}
}
//***************************

Any other suggestions are welcome
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top