Question Datatables & Listbox problem part 1

BillO

Member
Joined
Sep 20, 2010
Messages
9
Programming Experience
Beginner
Hi VB Legends,

I've been struggling with a program to access a database with three tables and present some of the information in a couple of listboxes.

I'm afraid my brains turned to mush and I can't find anything that seems to help. Any assistance very welcome.

When I push the btnOutOfStock, The heading doesn't print and I get a bunch of System.DataRow.Views in the listbox. Obviously grabbed the wrong end of the box somewhere!

The relevant code is below:
VB.NET:
   Private Sub btnOutOfStock_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnOutOfStock.Click
       Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
       Dim dataAdapter2 As New OleDb.OleDbDataAdapter(sqlStr2, connStr)
       Dim qtyInStock As Double
       Dim fmtStr As String = "{0, -40}"
       lstStock.Items.Add(String.Format(fmtStr, "Items that are currently
out of stock."))
       lstStock.Items.Add(String.Format(fmtStr, "--------------------------------------"))
       dataAdapter.Fill(dt)
       dataAdapter2.Fill(dt2)
       dataAdapter.Dispose()
       lstStock.DataSource = dt
       lstStock.DisplayMember = "Name"
       For i = 0 To dt2.Rows.Count - 1
           qtyInStock = dt2.Rows(i)("quantity")
           For j = 0 To dt.Rows.Count - 1
               If dt2.Rows(i)("itemID") = dt.Rows(j)("itemID") Then
                   qtyInStock -= dt.Rows(i)("quantity")
               End If
           Next
           If qtyInStock <= 0 Then
               lstStock.DisplayMember = dt2.Rows(i)("description")
           End If
       Next
   End Sub
Many thanks!
 
Last edited by a moderator:
If you are going to bind data to a control then you can't add items directly. It's an either/or thing.

Do you have to use a ListBox? It's really the wrong choice for tabular data but, unfortunately, sometimes students are forced to use it for an assignment.
 
Listboxes in a bind!

Hi jmcilhinney,

Thanks for your prompt reply. I've been trying to get a website up and running all weekend. Unfortunately I am forced to use a listbox to display. It would be preferable to use a datagridview. I am unsure how I can implement headings, (unless I add the headings into cells the table which sounds very messy) and the data given what you are saying?

Is this problem afflicting the second part also?

cheers ;o)
 
All fixed

Someone told me what was going on - display.member is what you meant by binding, all I needed to use is lstbox.add.item only.

Cheers!
 

Latest posts

Back
Top