JohnDW
Well-known member
Dear Forummembers,
I have a listbox with data and a list view. I want that when the user selects an item from the listbox, the listview is filled with a selection of the data that the user clicked in the listbox.
The code to fill the listbox (string as data):
the code that fills the listview
The problem is the syntax from myCommand.
Someone a hint?
JOhn
I have a listbox with data and a list view. I want that when the user selects an item from the listbox, the listview is filled with a selection of the data that the user clicked in the listbox.
The code to fill the listbox (string as data):
VB.NET:
Private Sub Categorie_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Cn As SqlConnection
Cn = New SqlConnection(Cnst)
Try
Cn.Open()
Dim myCommand As SqlCommand
myCommand = New SqlCommand("Select CategorieId from Categorie order by categorieId asc", Cn)
Dim dr As SqlDataReader
dr = myCommand.ExecuteReader
Dim i As Integer
i = 0
While dr.Read()
i = +i
ListBox1.Items.Add(dr(i).ToString)
End While
dr.Close()
Cn.Close()
Catch ex As Exception
End Try
End Sub
VB.NET:
Dim sect As String
sect = CStr(ListBox1.SelectedItem)
MsgBox(sect)
Dim Cn As SqlConnection
Cn = New SqlConnection(Cnst)
Try
ListView1.Items.Clear()
ListView1.View = View.Details
ListView1.Columns.Add("Productomschrijving", 10, HorizontalAlignment.Left)
ListView1.Columns.Add("CategorieId", 10, HorizontalAlignment.Left)
ListView1.Columns.Add("Productnaam", 10, HorizontalAlignment.Left)
ListView1.Columns.Add("KleurId", 10, HorizontalAlignment.Left)
ListView1.Columns.Add("MaatId", 10, HorizontalAlignment.Left)
ListView1.Columns.Add("Basis", 10, HorizontalAlignment.Left)
ListView1.Columns.Add("Serienummer", 10, HorizontalAlignment.Left)
ListView1.Columns.Add("Scannummer", 10, HorizontalAlignment.Left)
Cn.Open()
Dim myCommand As SqlCommand
' when I use the myCOmmand syntax below the listview loads very quick
' myCommand = New SqlCommand("Select Productomschrijving, CategorieId, Productnaam, KleurId, MaatId, Basis, Serienummer, Scannummer from Product where CategorieId = 'anita'", Cn)
' when I use the mycommand syntax below, the listview doesn't load
myCommand = New SqlCommand("Select Productomschrijving, CategorieId, Productnaam, KleurId, MaatId, Basis, Serienummer, Scannummer from Product where CategorieId =" & sect, Cn)
Dim dr As SqlDataReader = myCommand.ExecuteReader
Do While dr.Read()
Dim new_item As New _
ListViewItem(dr.Item("Productomschrijving").ToString)
new_item.SubItems.Add(dr.Item("CategorieId").ToString)
new_item.SubItems.Add(dr.Item("Productnaam").ToString)
new_item.SubItems.Add(dr.Item("KleurId").ToString)
new_item.SubItems.Add(dr.Item("MaatId").ToString)
new_item.SubItems.Add(dr.Item("Basis").ToString)
new_item.SubItems.Add(dr.Item("Serienummer").ToString)
new_item.SubItems.Add(dr.Item("Scannummer").ToString)
ListView1.Items.Add(new_item)
Debug.WriteLine(new_item.Text & " : " & _
new_item.SubItems(0).Text & ", " & _
new_item.SubItems(1).Text & ", " & _
new_item.SubItems(2).Text & ", " & _
new_item.SubItems(3).Text & ", " & _
new_item.SubItems(4).Text & ", " & _
new_item.SubItems(5).Text & ", " & _
new_item.SubItems(6).Text)
Loop
dr.Close()
Cn.Close()
Dim wid As Integer
For i As Integer = 0 To ListView1.Columns.Count - 1
ListView1.Columns(i).Width = -2
wid += ListView1.Columns(i).Width
Next i
Me.ClientSize = New Size(wid + 20, Me.ClientSize.Height)
Catch ex As Exception
End Try
End Sub
Someone a hint?
JOhn