2

我之前问过这个问题,但建议提供更多细节。这是问题所在:

  1. 我有一个名为CheckedList_Facility. 此处的所有项目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()
    
  2. 我想获取已检查项目的列表。例如,

    [X] AAA

    [X] BBB

    [ ] CCC

    [ ] DDD

    [X] 电子电气设备

那么列表应该是“AAA”、“BBB”、“EEE”

  1. 为了测试项目是否被正确检索,我使用了一个按钮调用bt_GetItem,当按下这个按钮时,一个 msgbox 会显示被检查的项目。使用此代码:

    Dim itemChecked As Object
    For Each itemChecked In CheckedList_Facility.CheckedItems
        MsgBox(itemChecked.ToString)
    Next
    
  2. 但是,我只收到此错误消息

    System.Data.Common.DataRecordInternal

从技术上讲,这可能不是错误,但我没有收到“AAA”,而是得到了这个

System.Data.Common.DataRecordInternal
4

4 回答 4

4

为了显示您的“AAA”(字符串类型),您必须访问itemChecked对象的属性。由于您选择“设施”,我们将使用它。您收到的消息(System.Data.Common.DataRecordInternal)是对象itemChecked类型。

MsgBox(itemChecked.Items("Facility").ToString)

应该输出“AAA”

于 2013-07-10T17:21:06.973 回答
4

因为您将选中列表框绑定到数据读取器,所以内部选中的对象实际上是一个{System.Data.Common.DataRecordInternal}而不是字符串或任何其他本机对象。您必须访问对象中的item属性才能获得所需的字符串,如下所示:

 MsgBox(itemChecked.item("Facility").ToString)
于 2013-07-10T17:08:29.290 回答
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
于 2016-06-20T07:50:24.723 回答
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
于 2015-10-09T22:01:23.723 回答