Results 1 to 6 of 6

Thread: [RESOLVED] XML file and Combobox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    28

    Resolved [RESOLVED] XML file and Combobox

    Good morning,

    I am sitting in front of my first Visual Basic project. My intention is to create a small application reading values from an XML file.

    My XML file

    <?xml version="1.0" encoding="utf-8"?>
    <Materialien>
    <Material name="Aluminium">
    <Dichte>2</Dichte>
    <EModul>70000</EModul>
    </Material>
    .......
    </Materialien>


    My VB Code snippet

    _dsData.ReadXml("C:\Material.xml")
    ComboBox1.DataSource = _dsData.Tables("Material")
    ComboBox1.DisplayMember = "name"

    This is working perfectly and I can choose a material from the XML file. So if I choose "Aluminium" in the combobox I would like to write "Dichte" and "EModul" into variables. I want to use them inside some calculations or just display them as Label2 and Label3.

    Any idea?

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: XML file and Combobox

    Hi,

    You can do this by Casting the SelectedItem of the ComboBox to the DataRowView Class. This then becomes a representation of the record held within the bound Table in the DataSet. i.e:-

    Code:
    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
      Dim myDataRow As DataRowView = DirectCast(ComboBox1.SelectedItem, DataRowView)
      Label1.Text = myDataRow(0).ToString
      Label2.Text = myDataRow(1).ToString
    End Sub
    Hope that helps.

    Cheers,

    Ian

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    28

    Re: XML file and Combobox

    Working perfectly! Thanks a lot!

    Is it possible to assign "myDataRow(0)" to a variable for further math i.e:

    myDataRow(0) + 1 (or something else?)

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: XML file and Combobox

    Hi,

    Yes, you can simply assign the value to a variable. i.e:-

    Code:
    Dim myValue As Integer = CInt(myDataRow(0))
    Hope that helps.

    Cheers,

    Ian

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    28

    Re: XML file and Combobox

    Getting an "Invalid CastException" Error

    Edit

    Well I guess there is a problem converting the XML value from a string into an integer
    Last edited by Moritz83; Apr 27th, 2013 at 06:05 AM.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    28

    Re: XML file and Combobox

    okay, I got it working with

    Code:
    Dim Dichte1 As String = myDataRow(0)
    Dim Zahl As Integer
    Zahl = Val(Dichte1) + 5

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width