Hey guys,

Having a bit of a problem. I have loaded a list of values from a mysql database into a CheckedListBox and want to be able to select them to eb used in a query. The CheckedListBox shows the data as table_name.attribute_name, and when an item is selected I try to break up the two values by using a substring, like this:

Dim strTable As String = clbFields.GetItemText(clbFields.SelectedItem.Substring(0, clbFields.SelectedItem.IndexOf("."))).ToString
        Dim strAttribute As String = clbFields.GetItemText(clbFields.SelectedItem).ToString

Whenever I select an item though it comes up with the error "Public member 'IndexOf' on type 'DataRowView' not found."
What needs to be changed?

Thanks in advance :)

Dani AI

Generated

Briefly: the exception happens because the CheckedListBox is data-bound and its items are DataRowView objects, not plain strings. DataRowView does not expose string methods like IndexOf or Substring, so calling those on SelectedItem fails. was right to suspect a DataRowView, and resolved it by converting the item into a string first. Below are safer, reusable patterns you can apply.

Cast the selected item to DataRowView and pull the display column, then split the text (prefer Split over manual index/substring for clarity and safety):

Dim drv As DataRowView = TryCast(clbFields.SelectedItem, DataRowView)
If drv IsNot Nothing Then
    Dim displayCol As String = clbFields.DisplayMember ' replace if you use a different column name
    Dim fullText As String = drv(displayCol).ToString()
    Dim parts() As String = fullText.Split("."c)
    Dim tableName As String = If(parts.Length > 0, parts(0), "")
    Dim attribute As String = If(parts.Length > 1, parts(1), "")
End If

If you need all checked entries, iterate CheckedItems (each entry will be a DataRowView when bound):

For Each itm As Object In clbFields.CheckedItems
    Dim drv As DataRowView = TryCast(itm, DataRowView)
    Dim fullText As String = If(drv IsNot Nothing, drv(clbFields.DisplayMember).ToString(), itm.ToString())
    Dim parts() As String = fullText.Split("."c)
    ' use parts(0) and parts(1) with null/length checks
Next

Quick tips: always null-check SelectedItem/CheckedItems; handle strings without a dot; if you respond to the ItemCheck event remember CheckedItems is not updated yet (use e.NewValue or defer handling with BeginInvoke). These approaches avoid the DataRowView/String type mismatch and make your parsing robust.

Recommended Answers

All 2 Replies

Post ur complete code... are u trying in datarow view? Cant you get the value to string variable and do indexof(.)?

Ok, yeah, I was being a bit slow there, sorry lol.

I had a variable that was getting the whole thing and had it as a string after where I was trying to break it up, so I've put that so it's being set before and just used it from that variable.

Thanks for your reply :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.