使用Access数据库填充列表框?

时间:2011-07-08 04:27:18

标签: vb.net visual-studio-2010 ms-access

我无法从我的数据库中获取列表框显示值。这是代码。关于如何破解它的任何想法?

Imports System.Data.OleDb
Public Class Form1
    Dim dbConnection As OleDbConnection
    Dim dbCommand As OleDbCommand
    Dim strInsert As String
    Dim dbDataAdapter As OleDbDataAdapter
    Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source =atg.mdb"
    Dim dtATG As DataTable

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dtTest As New DataTable
        dtTest.Columns.Add("Col1", GetType(Integer))


        For i As Integer = 1 To 10
            dtTest.Rows.Add(i, "Row " & i.ToString)
        Next

        ListBox1.DisplayMember = "Col1"
        ListBox1.ValueMember = "Col1"
        ListBox1.DataSource = dtTest.DefaultView
    AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged

End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    MessageBox.Show(ListBox1.SelectedValue.ToString)

End Sub

结束班

2 个答案:

答案 0 :(得分:1)

以下是您的代码的修改版本。

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim dtTest As New DataTable
        dtTest.Columns.Add("Col1", GetType(Integer))


        For i As Integer = 1 To 10
            dtTest.Rows.Add(i)
        Next

        ListBox1.DisplayMember = "Col1"
        ListBox1.ValueMember = "Col1"

        ListBox1.DataSource = dtTest
End Sub

答案 1 :(得分:0)

使用System.IO.File.ReadAllLines

ListBox1.Items.AddRange(System.IO.File.ReadAllLines("C:\folder\Your File.txt"))