之前我问过这个问题,但建议提供更多细节。这是问题所在:
我有一个名为CheckedList_Facility
的CheckedListBox。此CheckedList_Facility
中的所有项都来自SQL Server数据源。使用以下代码正确加载所有项目
Dim queryString As String = "SELECT Facility FROM Database.dbo.Facility "
Dim connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(queryString, connection)
connection.Open()
Dim dataReader As SqlDataReader = command.ExecuteReader()
Dim source As New BindingSource
source.DataSource = dataReader
CheckedList_Facility.DataSource = source
CheckedList_Facility.ValueMember = "Facility"
connection.Close()
我想获取一系列已检查的项目。例如,
[X] AAA
[X] BBB
[] CCC
[] DDD
[X] EEE
然后列表应为“AAA”,“BBB”,“EEE”
要测试是否正确检索了项目,我使用按钮调用bt_GetItem
,当按下此按钮时,msgbox会显示已检查的项目。使用此代码:
Dim itemChecked As Object
For Each itemChecked In CheckedList_Facility.CheckedItems
MsgBox(itemChecked.ToString)
Next
但是,我只收到此错误消息
System.Data.Common.DataRecordInternal
从技术上讲,这可能不是错误,但我没有收到“AAA”,而是得到了这个
System.Data.Common.DataRecordInternal
答案 0 :(得分:4)
因为您将checkedlistbox绑定到datareader,所以内部检查的对象实际上是{System.Data.Common.DataRecordInternal}
而不是字符串或任何其他本机对象。
您必须访问对象中的item
属性才能获得所需的字符串,如下所示:
MsgBox(itemChecked.item("Facility").ToString)
答案 1 :(得分:4)
要显示“AAA”(字符串类型),您必须访问itemChecked
对象的属性。由于您选择“设施”,我们将使用它。您收到的消息( System.Data.Common.DataRecordInternal )是对象itemChecked
类型。
MsgBox(itemChecked.Items("Facility").ToString)
应输出“AAA”
答案 2 :(得分:0)
For i As Integer = 0 To lbSit.Items.Count - 1
If CType(lbSit.Items(i), DataRowView)(lbSit.ValueMember).ToString = "32" Then
lbSit.SetItemChecked(i, True)
End If
Next
答案 3 :(得分:0)
Private Sub WhatIsChecked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WhatIsChecked.Click
' Display in a message box all the items that are checked.
Dim indexChecked As Integer
Dim itemChecked As Object
Const quote As String = """"
' First show the index and check state of all selected items.
For Each indexChecked In CheckedListBox1.CheckedIndices
' The indexChecked variable contains the index of the item.
MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" + _
CheckedListBox1.GetItemCheckState(indexChecked).ToString() + ".")
Next
' Next show the object title and check state for each item selected.
For Each itemChecked In CheckedListBox1.CheckedItems
' Use the IndexOf method to get the index of an item.
MessageBox.Show("Item with title: " + quote + itemChecked.ToString() + quote + _
", is checked. Checked state is: " + _
CheckedListBox1.GetItemCheckState(CheckedListBox1.Items.IndexOf(itemChecked)).ToString() + ".")
Next