View Access 2007 Database Table Contents using VB.net
I am trying to display the contents of a table from an Access 2007 Database inside a List-view Box.
I am using a Combo Box to select the user I wish to see the details about:
Private Sub Admin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ReadUsers()
con.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=E:\Computing\Projects\Login\Login\bds.accdb;")
con.Open()
Sql = "Select * From Clients"
da = New OleDb.OleDbDataAdapter(Sql, con)
da.Fill(ds, "Clients")
MaxRows = ds.Tables("Clients").Rows.Count
intCounter = -1
ComboBoxClients.DisplayMember = "ClientName"
Co开发者_开发问答mboBoxClients.ValueMember = "ID"
ComboBoxClients.DataSource = ds.Tables("Clients")
Then inside the List View:
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Dim Test As Integer
ListView1.Clear()
For i = 0 To MaxRows - 1
Test = ds.Tables("Clients").Rows(i).Item("ClientName")
If Test = ComboBoxClients.SelectedValue Then
For intCounter As Integer = 0 To ds.Tables("Clients").Columns.Count - 1
ListView1.Columns.Add(ds.Tables("Clients").Columns(intCounter).ColumnName, 80, HorizontalAlignment.Center)
Next
Dim ListItem = New ListViewItem
ListItem = ListView1.Items.Add(ds.Tables("Clients").Rows(i).Item(0))
ListItem.SubItems.Add(ds.Tables("Clients").Rows(i).Item(1))
ListItem.SubItems.Add(ds.Tables("Clients").Rows(i).Item(2))
ListItem.SubItems.Add(ds.Tables("Clients").Rows(i).Item(3))
ListItem.SubItems.Add(ds.Tables("Clients").Rows(i).Item(4))
End If
Next
End Sub
The listview box currently doesn't display anything:
Any help would be great, thanks.
ListView1 Revision:
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Dim Test As Integer
Dim ListItem = New ListViewItem
ListView1.Clear()
For i = 0 To MaxRows - 1
Test = ds.Tables("Clients").Rows(i).Item("ClientName")
If Test = ComboBoxClients.SelectedValue Then
For intCounter As Integer = 0 To ds.Tables("Clients").Columns.Count - 1
ListView1.Columns.Add(ds.Tables("Clients").Columns(intCounter).ColumnName, 80, HorizontalAlignment.Center)
Next
ListItem.SubItems.Add(ds.Tables("Clients").Rows(i).Item(0))
ListItem.SubItems.Add(ds.Tables("Clients").Rows(i).Item(1))
ListItem.SubItems.Add(ds.Tables("Clients").Rows(i).Item(2))
ListItem.SubItems.Add(ds.Tables("Clients").Rows(i).Item(3))
ListItem.SubItems.Add(ds.Tables("Clients").Rows(i).Item(4))
ListView1.Items.Add(ListItem)
End If
Next
End Sub
This line ...
ListItem = ListView1.Items.Add(ds.Tables("Clients").Rows(i).Item(0))
... is not actually adding the ListItem to ListView1. After you declare and set up ListItem, then add it to the ListView control:
' ...
ListItem.SubItems.Add(ds.Tables("Clients").Rows(i).Item(3))
ListItem.SubItems.Add(ds.Tables("Clients").Rows(i).Item(4))
ListView1.Items.Add(ListItem)
Alternatively, I've found it easier to populate a ListView control like this:
Dim items() As String = {dr("customer_id").ToString, _
dr("id").ToString, _
dr("name").ToString, _
dr("address1").ToString, _
dr("city").ToString, _
dr("state").ToString, _
dr("zip_code").ToString}
Dim lvi As New ListViewItem(items)
ListView1.Items.Add(lvi)
精彩评论