Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » .NET » C#
Language: C#
Expertise: Beginner
Jan 30, 2006

Access the ValueMember Item in a Bound CheckedListBox

This example retrieves items in a list that have been checked and simply outputs them to the Output window.

Assume the list has been bound to a DataSource, like a DataTable, and that the DisplayMember and ValueMember properties are bound to fields in the DataTable:


    myCheckedList.DataSource = myDataTable;
    myCheckedList.DisplayMember = "FieldNameOne";
    myCheckedList.ValueMember = "FieldNameTwo";
To read the value associated with FieldNameTwo for items in the CheckedListBox, employ a DataRowView object to retrieve the value:

    foreach(DataRowView view in myCheckedList.CheckedItems)
    {
        Console.WriteLine(view[myCheckedList.ValueMember].ToString());
    } 
As you can see, the foreach automatically casts CheckedItemCollection's Item objects into DataRowView objects as it iterates through the collection. This allows you to retreive the value you want.

On a side note, you could have used the literal string FieldNameTwo instead of myCheckedList.ValueMember as the array indexer for the view, but using the latter lends you a bit more flexibility—in case you want to change the field (or the name of the field) to which the CheckedListBox ValueMember is linked.

Evan Stone
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap