ieg
asked on
c#How to populate a winform drop down box with XML data
I would like to populate a drop down box (list or combo) with multiple columns of data from an XML file. Could someone advise on the best technique please.
The XML looks like this
<root>
<Form Name='F1' Description='Test Form 1'/>
<Form Name='F2' Description='Test Form 2'/>
</root>
I have loaded the XML into a DOM and have created a nodelist of the Form Elements which I can iterate over. The bit I can't get my head around is the commands for adding the data.
Thanks
Andy
The XML looks like this
<root>
<Form Name='F1' Description='Test Form 1'/>
<Form Name='F2' Description='Test Form 2'/>
</root>
I have loaded the XML into a DOM and have created a nodelist of the Form Elements which I can iterate over. The bit I can't get my head around is the commands for adding the data.
Thanks
Andy
ASKER
Hi Peter,
Thanks, this looks good - how would I add a second column?
Andy
Thanks, this looks good - how would I add a second column?
Andy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You may also use data binding approach. It better suites for multicolumn controls (like grids).
However it's not possible for a standard combo.
Still, have a look at the snippet. If you go with dataviewgrid you don't need the last line.
However it's not possible for a standard combo.
Still, have a look at the snippet. If you go with dataviewgrid you don't need the last line.
private void button3_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml("XMLFile1.xml");
DataTable dt = ds.Tables[0];
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "Name";
}
ASKER
Many thanks for your replies. I just find it odd that at the very least a combo box would require 2 columns, a key field and a description. Even in MS Access this is a trivial task.
I fear that I will have to adopt a third party control.
I fear that I will have to adopt a third party control.
You could have the description (for each item) in the tooltip. But that would also require a 3rd party control or your own extension to the standard control.
Is it this you're looking for?
XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<root>
<Form Name='F1' Description='Test Form 1'/>
<Form Name='F2' Description='Test Form 2'/>
</root>
");
foreach (XmlNode node in doc.DocumentElement.ChildN
comboBox1.Items.Add(node.A
}
/peter