Binding XML data to a combobox and get the selected value

  • Thread starter Thread starter Marko Maehner
  • Start date Start date
M

Marko Maehner

Hi!

I have problems with databinding of xml files to comboboxes and then
getting the selected value of a selected item. For example, I have the
following xmlfile:


<?xml version="1.0" encoding="UTF-8" ?>
<Foo xmlns="http://tempuri.org/test.xsd">
<Person xmlns="http://tempuri.org/test.xsd">
<number>114</number>
<firstname>John</firstname>
<lastname>Doe</lastname>
</Mitarbeiter>
</Foo>

1. Can anybody help me (with a snipplet of code) to fill a combobox
(Windows form) with both names (first AND last name as displayed
members) and number as a valuemember?

2. When an item of this cbx has been selected a function is called to
show the selected VALUE (In this case number 114)?
Can anybody show me this line of code to get the selected value?

Hope anyone can help me with one of these problems because I have
tried so much and nothing worked correctly...


Thanks in advance,
Marko
 
Hi Marko,

1. Code for filling the combobox with single DisplayMember is pretty simple:

DataSet dsData = new DataSet ();
dsData.ReadXml ("test.xml");

comboBox1.DataSource = dsData.Tables[0];
comboBox1.DisplayMember = "firstname"
comboBox1.ValueMember = "number";

For multi-column ComboBox I suggest you to read this: http://www.codeproject.com/cs/combobox/multicolumncombo.as

2. Here is how you get the selected value;
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e

string str = comboBox1.SelectedValue.ToString ()
MessageBox.Show (str);



Hope it helps

Regards
Peter
 
Back
Top