devxlogo

Access the ValueMember Item in a Bound CheckedListBox

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.

See also  How IoT Tech Will Transform Logistics
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist