中检索选中的项目

中检索选中的项目

本文介绍了从 CheckedListBox VB.NET Winform 中检索选中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前问过这个问题,但建议提供更多细节.问题来了:

I asked this question before but suggested to provide a more details. Here is the problem:

  1. 我有一个名为 CheckedList_Facility 的 CheckedListBox.此 CheckedList_Facility 中的所有项目都来自 SQL Server 数据源.使用以下代码正确加载所有项目

  1. I have a CheckedListBox called CheckedList_Facility. All items in this CheckedList_Facility are getting from SQL Server Datasource. All items are loaded properly using the below code

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()

  • 我想得到一个被检查项目的列表.例如,

  • I would like to get a list of items that are checked. For example,

    [X] AAA

    [X] BBB

    [ ] CCC

    [ ] DDD

    [X] EEE

    那么列表应该是AAA"、BBB"、EEE"

    then the list should be "AAA", "BBB", "EEE"

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

    1. To test if the item is retrieved correctly, I using a button call bt_GetItem and when this button is pressed, a msgbox displays the items that are checked. With this code:

    Dim itemChecked As Object
    For Each itemChecked In CheckedList_Facility.CheckedItems
        MsgBox(itemChecked.ToString)
    Next
    

  • 但是,我只收到此错误消息

  • However, I only receive this error message

    System.Data.Common.DataRecordInternal

    从技术上讲,这可能不是错误,但我收到的不是AAA",而是

    Technically, this might not be an error but instead of receiving "AAA", I get this

    System.Data.Common.DataRecordInternal
    

    推荐答案

    因为你绑定了checkedlistbox 到你的datareader,所以checked 对象内部实际上是一个{System.Data.Common.DataRecordInternal} 和不是字符串或任何其他本机对象.您必须访问对象中的 item 属性才能获得所需的字符串,如下所示:

    Because you bound the checkedlistbox to your datareader, the checked object internally is actually a {System.Data.Common.DataRecordInternal} and not a string or any other native object.You have to access the item property within the object to get to the string you want, like so:

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

    这篇关于从 CheckedListBox VB.NET Winform 中检索选中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-05 23:24