Link to home
Start Free TrialLog in
Avatar of sergeiweerasuriya
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_fname>
    <Cus_lname>ellison</Cus_lname>
    <Tel>0792438456</Tel>
    <Add_Line1>2</Add_Line1>
    <Add_Line2>shrewsbury road</Add_Line2>
    <Post_Code>st174dl</Post_Code>
    <Day_Read>175</Day_Read>
    <Night_Read>245</Night_Read>
  </Customers>
  <Customers>
    <Cus_fname>peter</Cus_fname>
    <Cus_lname>king</Cus_lname>
    <Tel>01785443224</Tel>
    <Add_Line1>34</Add_Line1>
    <Add_Line2>cramer street</Add_Line2>
    <Post_Code>st165dg</Post_Code>
    <Day_Read>150</Day_Read>
    <Night_Read>400</Night_Read>
  </Customers>
</NewDataSet>
Avatar of nayernaguib
nayernaguib
Flag of Egypt image

You can use the following code:

_______________________________________________

        For i = 0 To ds.Tables(0).Rows.Count
            If Not comboPostcodes.Items.Contains(ds.Tables(0).Rows(i)("Post_Code")) Then
                comboPostcodes.Items.Add(ds.Tables(0).Rows(i)("Post_Code"))
            End If
            If Not comboLine2.Items.Contains(ds.Tables(0).Rows(i)("Add_Line2")) Then
                comboLine2.Items.Add(ds.Tables(0).Rows(i)("Add_Line2"))
            End If
            If Not listLine1.Items.Contains(ds.Tables(0).Rows(i)("Add_Line1")) Then
                listLine1.Items.Add(ds.Tables(0).Rows(i)("Add_Line1"))
            End If
        Next

_______________________________________________

_______________

  Nayer Naguib
Avatar of sergeiweerasuriya
sergeiweerasuriya

ASKER

where do i write this? In Form Load?
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?
Avatar of toocrazy007
If ComboLine1.SelectedText="x" Then
'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
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(rowIndex)
            If Not cboPostcode.Items.Contains(ds.Tables(0).Rows(rowIndex)("Post_Code")) Then
                cboPostcode.Items.Add(ds.Tables(0).Rows(rowIndex)("Post_Code"))
            End If
        Next
    End Sub

    Private Sub cboPostcode_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboPostcode.SelectedIndexChanged
        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(rowIndex)
            If row("Post_Code") = cboPostcode.SelectedText Then
                If Not cboStreet.Items.Contains(ds.Tables(0).Rows(rowIndex)("Add_Line2")) Then
                    cboStreet.Items.Add(ds.Tables(0).Rows(rowIndex)("Add_Line2"))
                End If
            End If
        Next

    End Sub

    Private Sub cboStreet_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboStreet.SelectedIndexChanged
        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(rowIndex)
            If row("Post_Code") = cboPostcode.SelectedText And row("Add_Line2") = cboStreet.SelectedText Then
                lstStreetAddresses.Items.Add(ds.Tables(0).Rows(rowIndex)("Add_Line2"))
            End If
        Next
    End Sub
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
Avatar of nayernaguib
nayernaguib
Flag of Egypt 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
The code is finally working . thanks.