0
votes

Hi guys when i use this simple code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim cheq As New System.Text.StringBuilder
    For Each item In CheckedListBox1.CheckedItems
        cheq.Append(item)
        cheq.Append(" ")

    Next
    MessageBox.Show(" Your Checked Items are : " & cheq.ToString())
       End Sub

and wait for the message i find this resault: "Your Checked Items are : system.data.datarowview"

What is the problem?

2

2 Answers

0
votes

When you bind a DataTable, the data comes from its DefaultView, which is a DataView. Each item in a DataView is a DataRowView and, like most types, calling ToString on one will simply return the type name.

You are presumably setting the DisplayMember of your CheckedListBox in order to specify which column of your DataTable the values should be drawn from to be displayed in the control. If what you actually want is the text that is displayed for the item then you should be calling the GetItemText method:

Dim itemText = CheckedListBox1.CheckedItems.
                               Cast(Of Object).
                               Select(Function(o) CheckedListBox1.GetItemText(o))

MessageBox.Show("Checked Items: " & String.Join(", ", itemText))
0
votes

There is no problem in the code, only a mismatch of your expectations. This CheckedListBox is bound to a datatable. When a control binds to a datatable, it actually attaches to the DataView exposed by the DefaultView property, and a DataView is a colelction of DataRowView

Presumably you want to get access to the underlying DataRow, available via the Row property of the DataRowView. It might be mostly unnecessary; you can get values out of a DataRowView in the same way as you would a DataRow; if your DataRow represents a person with a Name column then either of these will give you the name:

DirectCast(DirectCast(item, DataRowView)("Name"), String)
DirectCast(DirectCast(item, DataRowView).Row("Name"), String)

Or to tidy it a little:

For Each item In CheckedListBox1.CheckedItems.Cast(Of DataRowView)
    cheq.Append(item("SOME_COLUMN_NAME_HERE"))
    cheq.Append(" ")

Next