vb.net - 为什么在 VB.NET 中没有出现 Binding Combobox usingdictionary with the MS ACCESS database with dapper

标签 vb.net dictionary combobox binding datasource

我正在尝试使用 VB.NET 中的 dapper 将字典与 MS ACCESS 数据库绑定(bind)组合框。

所以我希望使用字典的绑定(bind)出现在组合框中,它是表DictionarytestResult

我的代码实现可能有问题吗?

请指导我

谢谢

Public Class Form4
    Private bindingSource1 As BindingSource = Nothing
    Private connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\dapperdemo.accdb;Persist Security Info=False;"
    Private CBFullList As Dictionary(Of String, Integer)
  Private Sub BindcomboboxColorCode()
        Using conn = New OleDbConnection(connectionString)
            conn.Open()
            Dim sql = "SELECT * FROM Dictionarytest"
            Dim CBFullList = conn.Query(Of Dictionarytest)(sql).ToDictionary(Function(row) row.Result, Function(row) row.Key)
            conn.Close()
        End Using
        ComboBox1.DisplayMember = "Key"
        ComboBox1.ValueMember = "Result"
        ComboBox1.DropDownHeight = 80
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
        ComboBox1.AutoCompleteMode = AutoCompleteMode.None
        ComboBox1.AutoCompleteSource = AutoCompleteSource.None
        ComboBox1.DataSource = New BindingSource(CBFullList.ToList(), Nothing)
    End Sub
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CBFullList = New Dictionary(Of String, Integer)()
        BindcomboboxColorCode()
    End Sub
End Class
Public Class Dictionarytest
    Public Property Key() As Integer
    Public Property Result() As String
End Class

ms access 中的结果数据库

db in ms access

回答来自@jmcilhinney的更新代码

Public Class Form4
    Private bindingSource1 As BindingSource = Nothing
    Private connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\dapperdemo.accdb;Persist Security Info=False;"
    Private CBFullList As Dictionary(Of Integer, String)
 Private Sub BindcomboboxColorCode()
        Using conn = New OleDbConnection(connectionString)
            conn.Open()
            Dim sql = "SELECT * FROM Dictionarytest"
            Dim CBFullList = conn.Query(Of Dictionarytest)(sql).ToDictionary(Function(row) row.Key, Function(row) row.Result)
            conn.Close()
        End Using
With ComboBox1
            .DisplayMember = "Value"
            .ValueMember = "Key"
            .DataSource = CBFullList.ToArray()
        End With
    End Sub
 Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 BindcomboboxColorCode()
    End Sub
End Class

更新@dr.null的代码

Private Sub BindcomboboxColorCode()
        Using conn = New OleDbConnection(connectionString)
            conn.Open()
            Dim sql = "SELECT * FROM Dictionarytest"
CBFullList = conn.Query(Of Dictionarytest)(sql).ToDictionary(Function(row) row.Key, Function(row) row.Result)
End Using
        With ComboBox1
            .DisplayMember = "Value"
            .ValueMember = "Key"
                .DropDownHeight = 80
                .DropDownStyle = ComboBoxStyle.DropDown
                .AutoCompleteMode = AutoCompleteMode.None
                .AutoCompleteSource = AutoCompleteSource.None
                .DataSource = CBFullList.ToArray()

            End With

    End Sub

最佳答案

这里有两件事需要解决。首先,您的代码无法运行的具体原因。其次,您应该做什么才能使其发挥作用并更好地发挥作用。

它不起作用的具体原因是您绑定(bind)了一个空的Dictionary,而不是从查询中填充的字典。您在此处声明一个字段:

Private CBFullList As Dictionary(Of String, Integer)

然后,您毫无意义地创建一个空的字典并将其分配给此处的该字段:

CBFullList = New Dictionary(Of String, Integer)()

然后,您忽略该字段并将填充的 Dictionary 分配给立即超出此处范围的局部变量:

Dim CBFullList = conn.Query(Of Dictionarytest)(sql).ToDictionary(Function(row) row.Result, Function(row) row.Key)

然后,您可以绑定(bind)您根本不应该在此处创建的空Dictionary:

ComboBox1.DataSource = New BindingSource(CBFullList.ToList(), Nothing)

即使您通过将填充的 Dictionary 分配给字段而不是局部变量来解决此问题,您的代码仍然无法工作,因为您的绑定(bind)是错误的。

我会将查询结果的 KeyResult 属性映射到字典项。除非表的 Key 列中可能有重复的值,但考虑到名称,这会很奇怪。

Private CBFullList As Dictionary(Of Integer, String)
CBFullList = conn.Query(Of Dictionarytest)(sql).ToDictionary(Function(row) row.Key, Function(row) row.Result)

然后,我将显示 ComboBox 中项目的 Value:

With ComboBox1
    .DisplayMember = "Value"
    .ValueMember = "Key"
    .DataSource = CBFullList.ToArray()
End With

如果需要,您可以使用 BindingSource,但是,如果使用它有意义,您应该将其添加到设计器中的表单中。

关于vb.net - 为什么在 VB.NET 中没有出现 Binding Combobox usingdictionary with the MS ACCESS database with dapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77785237/

相关文章:

silverlight - Dataform Silverlight 中的数据绑定(bind)组合框在 Update 中使用 MVVM

vb.net - 发生 COMException - 未知名称。 (来自 HRESULT : 0x80020006 (DISP_E_UNKNOWNNAME)) when trying to modify an existing excel file 的异常

java - 如何测试字符串中仅包含字母或撇号?

使用 VB.NET 调用带有字符指针的非托管 C DLL

c# - Windows 窗体应用程序基础 : Keeping all forms in one window

python - 如何在使用 while 循环时将值 append 到字典中的列表?

html - 无法在 html 选项标签上添加图像

loops - range 子句针对 map 等不同的数据结构类型返回哪些不同的变量?

Python Json 和 Python 的多线程/进程组合?

包含字节的 JavaFX ComboBox