• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

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>
0
sergeiweerasuriya
Asked:
sergeiweerasuriya
  • 5
  • 3
1 Solution
 
nayernaguibCommented:
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
0
 
sergeiweerasuriyaAuthor Commented:
where do i write this? In Form Load?
0
 
sergeiweerasuriyaAuthor Commented:
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?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
toocrazy007Commented:
If ComboLine1.SelectedText="x" Then
'Statements that you want to fire when the condition is true
End if
0
 
nayernaguibCommented:
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
0
 
sergeiweerasuriyaAuthor Commented:
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
0
 
sergeiweerasuriyaAuthor Commented:
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.
0
 
nayernaguibCommented:
Use the ComboBox.Text property rather than ComboBox.SelectedText.

_______________

  Nayer Naguib
0
 
sergeiweerasuriyaAuthor Commented:
The code is finally working . thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now