vb.net 中组合框中的多个字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28216380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:44:05  来源:igfitidea点击:

multiple fields in a combobox in vb.net

vb.netms-access

提问by Boneyt

I have a form which contains a ComboBox linked to an access database. I am asking the combobox to display the Incident ID, Supplier and Supply date of all records in the database table

我有一个包含链接到访问数据库的 ComboBox 的表单。我要求组合框显示数据库表中所有记录的事件 ID、供应商和供应日期

The code is as follows

代码如下

Private Sub frm_5_UpdateIncidentSelect_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Links dropdown menu to incident table in database
    Dim dt As New DataTable
    Dim query As String = "select [incident ID],[stock supplier],[supply date] from incident"
    Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Database.accdb")
        Using command As New OleDbCommand(query, connection)
            Using adapter As New OleDbDataAdapter(command)
                connection.Open()
                adapter.Fill(dt)
                connection.Close()
            End Using
        End Using
    End Using
    Dim MyDataRow As DataRow = dt.Rows(0)
    Dim x As Integer
    x = dt.Rows.Count
    For y = 0 To x
        If y < x Then
            MyDataRow = dt.Rows(y)
            ComboBox1.Items.Add(CStr(MyDataRow("Incident ID")))
            ComboBox1.Items.Add(CStr(MyDataRow("stock supplier")))
            ComboBox1.Items.Add(CStr(MyDataRow("supply date")))
        End If
    Next
End Sub

The issue I am having is that the data is being returned over 3 lines as follows

我遇到的问题是数据通过 3 行返回,如下所示

12
Supplier1
01/01/2015
13
Supplier2
07/01/2015

Ideally I need this information returned on one as follows

理想情况下,我需要按如下方式返回此信息

12   Supplier    1    01/01/2015

13   Supplier    2    07/01/2015

I cannot for the life of me figure this out, I am not great with VB I am afraid. Can anyone tell me where I am going wrong?

我一生都无法弄清楚这一点,恐怕我对 VB 并不擅长。谁能告诉我我哪里出错了?

回答by Idle_Mind

Here's an example of what Plutonix was talking about:

以下是 Plutonix 所谈论内容的示例:

Public Class frm_5_UpdateIncidentSelect

    Private Class ComboBoxData

        Public IncidentID As String
        Public StockSupplier As String
        Public SupplyDate As String

        Public Sub New(ByVal data As DataRow)
            Me.IncidentID = data("Incident ID").ToString
            Me.StockSupplier = data("stock supplier").ToString
            Me.SupplyDate = data("supply date").ToString
        End Sub

        Public Overrides Function ToString() As String
            Return Me.IncidentID.PadRight(5, " ") & " Supplier " & Me.StockSupplier.PadRight(5, " ") & " " & Me.SupplyDate
        End Function

    End Class

    Private Sub frm_5_UpdateIncidentSelect_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Links dropdown menu to incident table in database
        Dim dt As New DataTable
        Dim query As String = "select [incident ID],[stock supplier],[supply date] from incident"
        Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Database.accdb")
            Using command As New OleDbCommand(query, connection)
                Using adapter As New OleDbDataAdapter(command)
                    connection.Open()
                    adapter.Fill(dt)
                    connection.Close()
                End Using
            End Using
        End Using
        For Each Data As DataRow In dt.Rows
            ComboBox1.Items.Add(New ComboBoxData(Data))
        Next
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedIndex <> -1 Then
            Dim data As ComboBoxData = DirectCast(ComboBox1.SelectedItem, ComboBoxData)
            Debug.Print(data.IncidentID)
            Debug.Print(data.StockSupplier)
            Debug.Print(data.SupplyDate)
        End If
    End Sub

End Class