I'm unable to get the listbox display values from my database.This is the code. ANy thoughts on how to crack it?
Imports System.Data.OleDb
Public Class Form1
Dim dbConnection As OleDbConnection
Dim dbCommand As OleDbCommand
Dim strInsert As Stri开发者_Go百科ng
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
End Class
Here is a modified version of your code that works.
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
Use the System.IO.File.ReadAllLines
:
ListBox1.Items.AddRange(System.IO.File.ReadAllLines("C:\folder\Your File.txt"))
精彩评论