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

问题描述 投票:0回答:1

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

所以我希望使用字典的绑定出现在组合框中,即表格的

Result
Dictionarytest

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

请指导我

谢谢

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 中的结果数据库

回答来自@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
vb.net dictionary combobox binding datasource
1个回答
2
投票

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

它不起作用的具体原因是您绑定了一个空的

Dictionary
而不是您从查询中填充的。您在这里声明一个字段:

Private CBFullList As Dictionary(Of String, Integer)

然后,您毫无意义地创建一个空的

Dictionary
并将其分配给此处的该字段:

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

然后,您忽略该字段并将填充的

Dictionary
分配给立即超出此处范围的局部变量:

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

然后绑定空的

Dictionary
,您甚至不应该在此处创建:

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

即使您通过将填充的

Dictionary
分配给字段而不是局部变量来解决此问题,您的代码仍然无法工作,因为您的绑定是错误的。

我会将查询结果的

Key
Result
属性映射到
Key
项的
Value
Dictionary
属性。除非表的
Key
列中可能有重复的值,但考虑到名称,这会很奇怪。

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

然后我会在

Value
:
 中显示项目的 
ComboBox

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

如果需要,您可以使用

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

© www.soinside.com 2019 - 2024. All rights reserved.