Link to home
Start Free TrialLog in
Avatar of Corey Scheich
Corey ScheichFlag for United States of America

asked on

ListBox Not populating on startup

I have a windows form with 2 listboxes ListCustomers and ListJobs.  The form connects to a Custom DataSet Class that is populated upon initialization. The schema is something like this

Table "Customers"
    Collumn "Customer"
    Collumn "Customer_ID"
Table "Jobs"
    Collumn "Job"
    Collumn "Job_ID
    Collumn "Customer_ID"

there is a datarelation between the tables using Customer_ID with Customers.Customer_ID being the parent

The Idea is to load the form populate a listbox with the customer names and then inturn populate another listbox with the jobs related to that customer.  The problem I am having is that when the form initializes the customer name is set to the first in the list but th Job list isn't getting populated properly.  What baffels me is that if I manually select the customer after the form loads The job list populates properly.

TIA

Corey2



Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System.Threading


Namespace DefaultNamespace
     
Public Class MainForm
     Inherits System.Windows.Forms.Form
'other controls
Private listJobs As System.Windows.Forms.ComboBox

Private ListCustomers As System.Windows.Forms.ComboBox

          Public Shared Sub Main
               Dim fMainForm As New MainForm
               fMainForm.ShowDialog()
               
          End Sub
         
          Public Sub New()
               MyBase.New
               '
               ' The Me.InitializeComponent call is
               'required for Windows Forms designer support.
               '
               Me.InitializeComponent
               Me.InitializeData
               '
               ' TODO : Add constructor code after InitializeComponents
               '
          End Sub
         
Private  Sub InitializeData ()
     dim tempTable as system.Data.DataTable
               
     db = new databasehandler
     Db.CreateOrConnect("C:\newdb.xml")
               

     listcustomers.DataSource = db.Tables("Customers")

     listcustomers.DisplayMember = "Customer"
     listcustomers.ValueMember = "Customer_ID"
               
     listcustomers.SelectedIndex = 0

               
End Sub
         
         
#Region " Windows Forms Designer generated code "

Private Sub InitializeComponent()
     Me.ListCustomers = New System.Windows.Forms.ComboBox
     Me.listJobs = New System.Windows.Forms.ComboBox
     'other Controls not relevent
     '
     'ListCustomers
     '
     Me.ListCustomers.Items.AddRange(New Object() {"Please Select a Job"})
     Me.ListCustomers.Location = New System.Drawing.Point(88, 8)
     Me.ListCustomers.Name = "ListCustomers"
     Me.ListCustomers.Size = New System.Drawing.Size(168, 21)
     Me.ListCustomers.TabIndex = 6
     Me.ListCustomers.Text = "Select a Customer"
     AddHandler Me.ListCustomers.SelectedIndexChanged, _
           AddressOf Me.ListCustomersSelectedIndexChanged
     'Other controls not relevent
     '
     'listJobs
     '
     Me.listJobs.Items.AddRange(New Object() {"Please Select a Job"})
     Me.listJobs.Location = New System.Drawing.Point(88, 40)
     Me.listJobs.Name = "listJobs"
     Me.listJobs.Size = New System.Drawing.Size(168, 21)
     Me.listJobs.TabIndex = 6
     Me.listJobs.Text = "Select a Job"
     '
     'MainForm
     '
     Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
     Me.CancelButton = Me.cmdCancel
     Me.ClientSize = New System.Drawing.Size(384, 254)
     Me.Controls.Add(Me.listJobs)
     'Add other controls
     Me.Controls.Add(Me.ListCustomers)
     'Add other controls
End Sub
#End Region

         
Private Sub ListCustomersSelectedIndexChanged _
     (sender As System.Object, e As System.EventArgs)

     Dim DataView as system.Data.DataView
     if Isnumeric(listcustomers.SelectedValue) andalso _
           cint(ListCustomers.SelectedValue) >= 0 then
          Dataview = New system.Data.DataView _
               (db.tables("Jobs"), _
               "Customer_ID = '" & listcustomers.SelectedValue & "'", _
               "Job",system.Data.DataViewRowState.CurrentRows)
               
          Listjobs.DataSource = Dataview
          ListJobs.DisplayMember = "Job"
     Else
          Listjobs.DataSource = Nothing
          listjobs.DisplayMember = ""
          Listjobs.Text = "Create a new Job"
         
     end if
End Sub

         


End Class
     
End Namespace

 
 
ASKER CERTIFIED SOLUTION
Avatar of RobertRFreeman
RobertRFreeman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Corey Scheich

ASKER

I'll let you know tomorrow  Thank you.
It's also better to use selectedvaluechanged instead of selectedindexchanged
Can I ask you another question I will give you more points for an answer? Another 500

I initially had these two lines inverted
     listcustomers.DisplayMember = "Customer"
     listcustomers.ValueMember = "Customer_ID"

Like this
     listcustomers.ValueMember = "Customer_ID"
     listcustomers.DisplayMember = "Customer"

But when the form loaded it showed the Customer_ID as the Display until I flipped them.  Do they really have to be in a certain order? if so is there documentation saying so?

Corey2
You should be able to specify them in either order.  The problem is that these are both specified after the data is loaded into the table.  If you set them first, then loaded the data, it wouldn't matter.

Generally, you should set these properties in the design window using a dataset dropped on the form.
Then you'd just do the fill in code.

And drop a dataview on your form as well instead of recreating it each time in the sub

Here are some databinding tips.
http://vsnetdatabinding.blogspot.com/


Thank you Robert.  That was exactly what I needed.  Now the form loads exactly as I expected.  I will post another Q to give you points for the secondary question.