Skip to main content Skip to footer

How to get the selected item in C1ComboBox SelectedIndexChanged event

When handling the SelectedIndexChanged event of a ComboBox, you can easily retrieve the current selection's index or the underlying value using the SelectedValue property. However, you may also need to dynamically fetch the corresponding text or model data from the DisplayMember column for that specific selection without manually scraping the screen text.

Solution
To access both the ValueMember and DisplayMember properties simultaneously during a selection change, you can cast the control's ItemsDataSource back to its original data source type, such as a DataTable or BindingSource.

Once you have access to the underlying data source, you can use the SelectedIndex of the ComboBox to locate the exact data row or object that was selected. From there, you can read any column or property—including the designated DisplayMember and ValueMember fields—ensuring that you are always pulling accurate, strongly typed data straight from the model rather than relying on UI strings.

Private Sub C1ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles C1ComboBox1.SelectedIndexChanged
    If C1ComboBox1.SelectedIndex = -1 Then Return

    Dim valueMember = C1ComboBox1.SelectedItem
    Dim displayMemberRow = TryCast(TryCast(sender, C1ComboBox).ItemsDataSource, DataTable).[Select]($"id={valueMember}")
    Dim displayMember = displayMemberRow(0)("name")

    Console.WriteLine($"Display Member: {displayMember}, Value Member: {valueMember}")
End Sub