Corey Scheich
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.Combo Box
Private ListCustomers As System.Windows.Forms.Combo Box
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:\new db.xml")
listcustomers.DataSource = db.Tables("Customers")
listcustomers.DisplayMembe r = "Customer"
listcustomers.ValueMember = "Customer_ID"
listcustomers.SelectedInde x = 0
End Sub
#Region " Windows Forms Designer generated code "
Private Sub InitializeComponent()
Me.ListCustomers = New System.Windows.Forms.Combo Box
Me.listJobs = New System.Windows.Forms.Combo Box
'other Controls not relevent
'
'ListCustomers
'
Me.ListCustomers.Items.Add Range(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.SelectedI ndexChange d, _
AddressOf Me.ListCustomersSelectedIn dexChanged
'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.listJob s)
'Add other controls
Me.Controls.Add(Me.ListCus tomers)
'Add other controls
End Sub
#End Region
Private Sub ListCustomersSelectedIndex Changed _
(sender As System.Object, e As System.EventArgs)
Dim DataView as system.Data.DataView
if Isnumeric(listcustomers.Se lectedValu e) andalso _
cint(ListCustomers.Selecte dValue) >= 0 then
Dataview = New system.Data.DataView _
(db.tables("Jobs"), _
"Customer_ID = '" & listcustomers.SelectedValu e & "'", _
"Job",system.Data.DataView RowState.C urrentRows )
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
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.Combo
Private ListCustomers As System.Windows.Forms.Combo
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:\new
listcustomers.DataSource = db.Tables("Customers")
listcustomers.DisplayMembe
listcustomers.ValueMember = "Customer_ID"
listcustomers.SelectedInde
End Sub
#Region " Windows Forms Designer generated code "
Private Sub InitializeComponent()
Me.ListCustomers = New System.Windows.Forms.Combo
Me.listJobs = New System.Windows.Forms.Combo
'other Controls not relevent
'
'ListCustomers
'
Me.ListCustomers.Items.Add
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.SelectedI
AddressOf Me.ListCustomersSelectedIn
'Other controls not relevent
'
'listJobs
'
Me.listJobs.Items.AddRange
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.listJob
'Add other controls
Me.Controls.Add(Me.ListCus
'Add other controls
End Sub
#End Region
Private Sub ListCustomersSelectedIndex
(sender As System.Object, e As System.EventArgs)
Dim DataView as system.Data.DataView
if Isnumeric(listcustomers.Se
cint(ListCustomers.Selecte
Dataview = New system.Data.DataView _
(db.tables("Jobs"), _
"Customer_ID = '" & listcustomers.SelectedValu
"Job",system.Data.DataView
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It's also better to use selectedvaluechanged instead of selectedindexchanged
ASKER
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.DisplayMembe r = "Customer"
listcustomers.ValueMember = "Customer_ID"
Like this
listcustomers.ValueMember = "Customer_ID"
listcustomers.DisplayMembe r = "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
I initially had these two lines inverted
listcustomers.DisplayMembe
listcustomers.ValueMember = "Customer_ID"
Like this
listcustomers.ValueMember = "Customer_ID"
listcustomers.DisplayMembe
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/
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/
ASKER
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.
ASKER