sergeiweerasuriya
asked on
Sorting a DataSet and passing values to a Combo box
I have an XML file of the following structure as given below: I have passed the xml data to a DataSet. I have two combo boxes on the form. The first combo box should list down all the post codes available(the same postcode should not repeat). Once you select the postcode the other combo box should list down all the "Add_Line2" values(the same value should not repeat). Then when i select the "Add_Line2" value from the combo box all the "Add_Line1" values should appear on a list box. I don't know how to do it. The only way i know how to do this by codingit in a way that all the post codes will be repeated in the postcodes combo box and so on....PLEASE HELP.
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Customers>
<Cus_fname>allan</Cus_fnam e>
<Cus_lname>ellison</Cus_ln ame>
<Tel>0792438456</Tel>
<Add_Line1>2</Add_Line1>
<Add_Line2>shrewsbury road</Add_Line2>
<Post_Code>st174dl</Post_C ode>
<Day_Read>175</Day_Read>
<Night_Read>245</Night_Rea d>
</Customers>
<Customers>
<Cus_fname>peter</Cus_fnam e>
<Cus_lname>king</Cus_lname >
<Tel>01785443224</Tel>
<Add_Line1>34</Add_Line1>
<Add_Line2>cramer street</Add_Line2>
<Post_Code>st165dg</Post_C ode>
<Day_Read>150</Day_Read>
<Night_Read>400</Night_Rea d>
</Customers>
</NewDataSet>
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Customers>
<Cus_fname>allan</Cus_fnam
<Cus_lname>ellison</Cus_ln
<Tel>0792438456</Tel>
<Add_Line1>2</Add_Line1>
<Add_Line2>shrewsbury road</Add_Line2>
<Post_Code>st174dl</Post_C
<Day_Read>175</Day_Read>
<Night_Read>245</Night_Rea
</Customers>
<Customers>
<Cus_fname>peter</Cus_fnam
<Cus_lname>king</Cus_lname
<Tel>01785443224</Tel>
<Add_Line1>34</Add_Line1>
<Add_Line2>cramer street</Add_Line2>
<Post_Code>st165dg</Post_C
<Day_Read>150</Day_Read>
<Night_Read>400</Night_Rea
</Customers>
</NewDataSet>
ASKER
where do i write this? In Form Load?
ASKER
I need to say "if the selected item in the combo box is x then do this" . What is the line of code for it?
If ComboLine1.SelectedText="x " Then
'Statements that you want to fire when the condition is true
End if
'Statements that you want to fire when the condition is true
End if
You can use the SelectedIndexChanged event handler of the combobox to execute code when the user makes a selection from a combobox.
To refer to the text of the selected item, use the SelectedText property of the combobox.
_______________
Nayer Naguib
To refer to the text of the selected item, use the SelectedText property of the combobox.
_______________
Nayer Naguib
ASKER
This is the code i have written below. Only the first combo box gets populated. why?
Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As DataSet = New DataSet
Dim Customers As DataTable
Dim rowIndex As Integer
Dim row As DataRow
ds.ReadXml("Customers.xml" )
Customers = ds.Tables.Item(0)
For rowIndex = 0 To Customers.Rows.Count - 1
row = Customers.Rows.Item(rowInd ex)
If Not cboPostcode.Items.Contains (ds.Tables (0).Rows(r owIndex)(" Post_Code" )) Then
cboPostcode.Items.Add(ds.T ables(0).R ows(rowInd ex)("Post_ Code"))
End If
Next
End Sub
Private Sub cboPostcode_SelectedIndexC hanged(ByV al sender As Object, ByVal e As System.EventArgs) Handles cboPostcode.SelectedIndexC hanged
Dim ds As DataSet = New DataSet
Dim Customers As DataTable
Dim rowIndex As Integer
Dim row As DataRow
ds.ReadXml("Customers.xml" )
Customers = ds.Tables.Item(0)
For rowIndex = 0 To Customers.Rows.Count - 1
row = Customers.Rows.Item(rowInd ex)
If row("Post_Code") = cboPostcode.SelectedText Then
If Not cboStreet.Items.Contains(d s.Tables(0 ).Rows(row Index)("Ad d_Line2")) Then
cboStreet.Items.Add(ds.Tab les(0).Row s(rowIndex )("Add_Lin e2"))
End If
End If
Next
End Sub
Private Sub cboStreet_SelectedIndexCha nged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboStreet.SelectedIndexCha nged
Dim ds As DataSet = New DataSet
Dim Customers As DataTable
Dim rowIndex As Integer
Dim row As DataRow
ds.ReadXml("Customers.xml" )
Customers = ds.Tables.Item(0)
For rowIndex = 0 To Customers.Rows.Count - 1
row = Customers.Rows.Item(rowInd ex)
If row("Post_Code") = cboPostcode.SelectedText And row("Add_Line2") = cboStreet.SelectedText Then
lstStreetAddresses.Items.A dd(ds.Tabl es(0).Rows (rowIndex) ("Add_Line 2"))
End If
Next
End Sub
Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As DataSet = New DataSet
Dim Customers As DataTable
Dim rowIndex As Integer
Dim row As DataRow
ds.ReadXml("Customers.xml"
Customers = ds.Tables.Item(0)
For rowIndex = 0 To Customers.Rows.Count - 1
row = Customers.Rows.Item(rowInd
If Not cboPostcode.Items.Contains
cboPostcode.Items.Add(ds.T
End If
Next
End Sub
Private Sub cboPostcode_SelectedIndexC
Dim ds As DataSet = New DataSet
Dim Customers As DataTable
Dim rowIndex As Integer
Dim row As DataRow
ds.ReadXml("Customers.xml"
Customers = ds.Tables.Item(0)
For rowIndex = 0 To Customers.Rows.Count - 1
row = Customers.Rows.Item(rowInd
If row("Post_Code") = cboPostcode.SelectedText Then
If Not cboStreet.Items.Contains(d
cboStreet.Items.Add(ds.Tab
End If
End If
Next
End Sub
Private Sub cboStreet_SelectedIndexCha
Dim ds As DataSet = New DataSet
Dim Customers As DataTable
Dim rowIndex As Integer
Dim row As DataRow
ds.ReadXml("Customers.xml"
Customers = ds.Tables.Item(0)
For rowIndex = 0 To Customers.Rows.Count - 1
row = Customers.Rows.Item(rowInd
If row("Post_Code") = cboPostcode.SelectedText And row("Add_Line2") = cboStreet.SelectedText Then
lstStreetAddresses.Items.A
End If
Next
End Sub
ASKER
The combo box postcode should be populated at the form load. The combo box street should be populated only when you make a selection from the postcode combo box. The list box should be populated only when a selection been made in combo box street.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The code is finally working . thanks.
__________________________
For i = 0 To ds.Tables(0).Rows.Count
If Not comboPostcodes.Items.Conta
comboPostcodes.Items.Add(d
End If
If Not comboLine2.Items.Contains(
comboLine2.Items.Add(ds.Ta
End If
If Not listLine1.Items.Contains(d
listLine1.Items.Add(ds.Tab
End If
Next
__________________________
_______________
Nayer Naguib