|
-
Apr 27th, 2013, 03:45 AM
#1
Thread Starter
Junior Member
[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?
-
Apr 27th, 2013, 04:18 AM
#2
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
-
Apr 27th, 2013, 04:54 AM
#3
Thread Starter
Junior Member
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?)
-
Apr 27th, 2013, 05:07 AM
#4
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
-
Apr 27th, 2013, 05:30 AM
#5
Thread Starter
Junior Member
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.
-
Apr 27th, 2013, 06:48 AM
#6
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|