A Comprehensive Guide to Retrieving Checked Item Values from CheckedListBox in C# WinForms

Dec 02, 2025 · Programming · 7 views · 7.8

Keywords: C# | WinForms | CheckedListBox | Data Binding | Type Conversion

Abstract: This article provides an in-depth exploration of how to effectively retrieve the text and values of checked items in a CheckedListBox control within C# WinForms applications. Focusing on the best answer (score 10.0), it details type conversion techniques in data-binding scenarios, including the use of DataRowView, strong-type casting, and the OfType extension method. Through step-by-step code examples, the guide demonstrates multiple approaches to extract CompanyName and ID fields from the CheckedItems collection, emphasizing type safety and error handling for comprehensive technical reference.

Introduction

In C# WinForms development, the CheckedListBox control is commonly used to allow users to select multiple items from a list. When the control is bound to a data source, as shown in the example:

chlCompanies.DataSource = dsCompanies.Tables[0];
chlCompanies.DisplayMember = "CompanyName";
chlCompanies.ValueMember = "ID";

developers often need to access the text (e.g., CompanyName) and values (e.g., ID) of the user-selected items. This guide explains how to achieve this based on the best answer.

Core Method: Type Conversion Using DataRowView

According to the best answer (score 10.0), when a CheckedListBox is bound to a DataTable, each item in the CheckedItems collection is actually a DataRowView object. Therefore, type conversion can be used to access the original data fields. Key steps include:

  1. Iterate through the CheckedItems collection: Use a foreach loop to process all checked items.
  2. Type conversion: Cast each item to DataRowView to access its column data.
  3. Extract values: Retrieve text and values via column names such as "CompanyName" and "ID".

Example code demonstration:

foreach(object itemChecked in checkedListBox1.CheckedItems)
{
    DataRowView castedItem = itemChecked as DataRowView;
    if (castedItem != null)
    {
        string companyName = castedItem["CompanyName"].ToString();
        int id = Convert.ToInt32(castedItem["ID"]);
        // Process companyName and id, e.g., display or store
    }
}

This method utilizes the as operator for safe conversion, avoiding InvalidCastException and handling null values if conversion fails.

Supplementary Methods: Alternative Data Access Techniques

Beyond the best answer, other responses offer additional insights. For example, using the DataRowExtensions.Field method for strongly-typed access:

foreach (var item in checkedListBox1.CheckedItems)
{
    var row = (item as DataRowView).Row;
    int id = row.Field<int>("ID");
    string name = row.Field<string>("CompanyName");
    // Use id and name
}

This approach enhances type safety and reduces runtime errors. Alternatively, if bound to a custom class (e.g., Company), the OfType extension method can be applied:

foreach (var company in checkedListBox1.CheckedItems.OfType<Company>())
{
    MessageBox.Show(company.Id + ": " + company.CompanyName);
}

This simplifies code by avoiding explicit type casts.

Practical Recommendations and Considerations

In real-world applications, consider the following points:

By integrating these methods, developers can flexibly extract data from checked items in a CheckedListBox, enhancing application interactivity and functionality.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.