locked
Data from XML to ComboBox then to label RRS feed

  • Question

  • Well here is my problem, I have an XML file and from there I have data going into a drop down ComboBox, what I need is the data from the next column over that corisponds to the same row, ie. the phone # that corisponds to a person's name in a data table. I semi-know what I need to do I just can't figure out the code to take the index of the ComboBox and take the data into the Label.

    Any help with the coding will be most apreciated, thank you in advance.

    Thursday, June 24, 2010 2:37 PM

Answers

  • Instead of reading the XML in as a DataTable, you can use LINQ to XML and bind to the results.

    I have an example here:

    http://msmvps.com/blogs/deborahk/archive/2009/10/20/populating-a-datagridview-from-xml-data.aspx

    It binds to a DataGridView, but you could use the same techniques to bind to a combobox.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Friday, June 25, 2010 6:07 PM
  • Hi NickO24,

    If you bind the two comboboxes to the same datatable and set the DisplayMember and ValueMember respectively, when you change the selected item in combobox1, the item(corresponding to the selecteditem in combobox1) in combobox2 will also be changed automaticlly. So you do not need to set "ComboBox2.SelectedIndex = ComboBox1.SelectedIndex" in ComboBox1_SelectedIndexChanged, you just need to handle ComboBox2's SelectedIndexChanged event and set the Lable's Text. Please try this:

    Private Sub Button_fill_drop_Click() Handles Button_fill_drop.Click
      If Fill_count = 0 Then
        Dim filePath As String = My.Application.Info.DirectoryPath & "\..\..\people.xml"
        test_fillDataSet.ReadXml(filePath)
    
        ComboBox1.DataSource = test_fillDataSet.Tables("test_fill")
        ComboBox1.DisplayMember = "name"
        ComboBox1.ValueMember = "name"
    
        ComboBox2.DataSource = test_fillDataSet.Tables("test_fill")
        ComboBox2.DisplayMember = "phone"
        ComboBox2.ValueMember = "phone"
        Fill_count = 1
      End If
    End Sub
    
    Private Sub ComboBox2_SelectedIndexChanged() Handles ComboBox2.SelectedIndexChanged
      If Fill_count = 1 Then
        Label2.Text = ComboBox2.Text
      End If
    End Sub
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, June 28, 2010 6:56 AM
    Moderator

All replies

  • Without seeing any code ... it is hard to envision exactly what you are doing.

    Are you using binding? If so, to what?

    If you are using Binding, you can use the BindingSource to determine the current "row" and get the other data from that row.

    Posting some code would help us help you.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Thursday, June 24, 2010 4:10 PM
  •  

     

    sorry forgot to include code... well i have it semi-working with a secondary non-visable combobox but there has to be a better way...

     Private Sub Button_fill_drop_Click() Handles Button_fill_drop.Click
    
    
    
    If Fill_count = 0 Then
     Dim filePath As String = My.Application.Info.DirectoryPath & "\..\..\people.xml"
    
     test_fillDataSet.ReadXml(filePath)
    
    
    
    
     ComboBox1.DataSource = test_fillDataSet.Tables("test_fill")
     ComboBox1.DisplayMember = "name"
     ComboBox1.ValueMember = "name"
    
     ComboBox2.DataSource = test_fillDataSet.Tables("test_fill")
     ComboBox2.DisplayMember = "phone"
     ComboBox2.ValueMember = "phone"
     
     Fill_count = 1
     End If
    
     End Sub
    
     Private Sub ComboBox1_SelectedIndexChanged() Handles ComboBox1.SelectedIndexChanged
    
     ComboBox2.SelectedIndex = ComboBox1.SelectedIndex
    
     If Fill_count = 1 Then
     Label2.Text = ComboBox2.Text
    
     End If
     End Sub

     

    Friday, June 25, 2010 1:45 AM
  • Instead of reading the XML in as a DataTable, you can use LINQ to XML and bind to the results.

    I have an example here:

    http://msmvps.com/blogs/deborahk/archive/2009/10/20/populating-a-datagridview-from-xml-data.aspx

    It binds to a DataGridView, but you could use the same techniques to bind to a combobox.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Friday, June 25, 2010 6:07 PM
  • Hi NickO24,

    If you bind the two comboboxes to the same datatable and set the DisplayMember and ValueMember respectively, when you change the selected item in combobox1, the item(corresponding to the selecteditem in combobox1) in combobox2 will also be changed automaticlly. So you do not need to set "ComboBox2.SelectedIndex = ComboBox1.SelectedIndex" in ComboBox1_SelectedIndexChanged, you just need to handle ComboBox2's SelectedIndexChanged event and set the Lable's Text. Please try this:

    Private Sub Button_fill_drop_Click() Handles Button_fill_drop.Click
      If Fill_count = 0 Then
        Dim filePath As String = My.Application.Info.DirectoryPath & "\..\..\people.xml"
        test_fillDataSet.ReadXml(filePath)
    
        ComboBox1.DataSource = test_fillDataSet.Tables("test_fill")
        ComboBox1.DisplayMember = "name"
        ComboBox1.ValueMember = "name"
    
        ComboBox2.DataSource = test_fillDataSet.Tables("test_fill")
        ComboBox2.DisplayMember = "phone"
        ComboBox2.ValueMember = "phone"
        Fill_count = 1
      End If
    End Sub
    
    Private Sub ComboBox2_SelectedIndexChanged() Handles ComboBox2.SelectedIndexChanged
      If Fill_count = 1 Then
        Label2.Text = ComboBox2.Text
      End If
    End Sub
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, June 28, 2010 6:56 AM
    Moderator