zorba111
asked on
C#: populate combobox with data (key:value) from an XML file.
I want to populate a combobox on a windows form with data from an XML file.
how to?
not much documentation, and I also searched past questions but no luck... all questions are in VB.net and/or more complex scenarios than what I need...
thanks!
how to?
not much documentation, and I also searched past questions but no luck... all questions are in VB.net and/or more complex scenarios than what I need...
thanks!
ASKER
Thanks saige....
Woah, some pretty complex stuff there for me!
my dataset:
sorry about formatting...
code I tried so far:
(this was in the form's "Load" event-handler)
and the output:
Woah, some pretty complex stuff there for me!
my dataset:
<?xml version="1.0" encoding="Windows-1252"?><JobCategories><JobCategories><JobCatID>8</JobCatID><JobCategory>Accountancy & Finance</JobCategory></JobCategories><JobCategories><JobCatID>20</JobCatID><JobCategory>Banking, Financial services & Insurance</JobCategory></JobCategories><JobCategories><JobCatID>11</JobCatID><JobCategory>Beauty,Hair Care,Leisure & Sport</JobCategory></JobCategories><JobCategories><JobCatID>9</JobCatID><JobCategory>Construction, Architecture & Property</JobCategory></JobCategories><JobCategories><JobCatID>23</JobCatID><JobCategory>Customer Service, Call Centres & Languages</JobCategory></JobCategories><JobCategories><JobCatID>13</JobCatID><JobCategory>Education, Childcare & Training</JobCategory></JobCategories><JobCategories><JobCatID>6</JobCatID><JobCategory>Engineering & Utilities</JobCategory></JobCategories><JobCategories><JobCatID>2</JobCatID><JobCategory>Environmental, Health & Safety</JobCategory></JobCategories><JobCategories><JobCatID>27</JobCatID><JobCategory>General Management</JobCategory></JobCategories><JobCategories><JobCatID>1</JobCatID><JobCategory>Hotel & Catering</JobCategory></JobCategories><JobCategories><JobCatID>16</JobCatID><JobCategory>HR & Recruitment</JobCategory></JobCategories><JobCategories><JobCatID>3</JobCatID><JobCategory>IT</JobCategory></JobCategories><JobCategories><JobCatID>4</JobCatID><JobCategory>Legal</JobCategory></JobCategories><JobCategories><JobCatID>21</JobCatID><JobCategory>Marketing</JobCategory></JobCategories><JobCategories><JobCatID>17</JobCatID><JobCategory>Nursing, Healthcare & Medical</JobCategory></JobCategories><JobCategories><JobCatID>7</JobCatID><JobCategory>Production, Manufacturing & Materials</JobCategory></JobCategories><JobCategories><JobCatID>115</JobCatID><JobCategory>Public Sector</JobCategory></JobCategories><JobCategories><JobCatID>25</JobCatID><JobCategory>Publishing, Media & Creative Arts</JobCategory></JobCategories><JobCategories><JobCatID>10</JobCatID><JobCategory>Retailing, Wholesaling & Purchasing</JobCategory></JobCategories><JobCategories><JobCatID>24</JobCatID><JobCategory>Sales</JobCategory></JobCategories><JobCategories><JobCatID>26</JobCatID><JobCategory>Science, Pharmaceutical & Food</JobCategory></JobCategories><JobCategories><JobCatID>114</JobCatID><JobCategory>Secretarial & Admin</JobCategory></JobCategories><JobCategories><JobCatID>14</JobCatID><JobCategory>Security, Trades & General Services</JobCategory></JobCategories><JobCategories><JobCatID>12</JobCatID><JobCategory>Social & Not for Profit</JobCategory></JobCategories><JobCategories><JobCatID>5</JobCatID><JobCategory>Telecoms</JobCategory></JobCategories><JobCategories><JobCatID>19</JobCatID><JobCategory>Tourism, Travel & Airlines</JobCategory></JobCategories><JobCategories><JobCatID>18</JobCatID><JobCategory>Transport, Warehousing & Motor</JobCategory></JobCategories><JobCategories><JobCatID>28</JobCatID><JobCategory>Graduate</JobCategory></JobCategories><JobCategories><JobCatID>29</JobCatID><JobCategory>Executive</JobCategory></JobCategories><JobCategories><JobCatID>116</JobCatID><JobCategory>Motoring</JobCategory></JobCategories></JobCategories>
sorry about formatting...
code I tried so far:
XmlDocument xmldocCategories = new XmlDocument();
xmldocCategories.Load("JobCategories.xml");
DataSet dsCategories = new DataSet();
dsCategories.ReadXml("JobCategories.xml");
//dsCategories.
cboCategories.DataSource = dsCategories.Tables[0];
//cboCategories.DataBind();
(this was in the form's "Load" event-handler)
and the output:
You were very close, the only thing you forgot was specifying your DisplayMember and ValueMember; e.g. -
Form1.cs -
Form1.cs -
using System;
using System.Data;
using System.Windows.Forms;
namespace EE_Q28708964
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataSet dsCategories = new DataSet();
dsCategories.ReadXml("JobCategories.xml");
cboCategories.DataSource = dsCategories.Tables[0];
cboCategories.DisplayMember = "JobCategory";
cboCategories.ValueMember = "JobCatID";
}
}
}
Form1.Designer.cs -
namespace EE_Q28708964
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cboCategories = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// cboCategories
//
this.cboCategories.FormattingEnabled = true;
this.cboCategories.Location = new System.Drawing.Point(13, 13);
this.cboCategories.Name = "cboCategories";
this.cboCategories.Size = new System.Drawing.Size(259, 21);
this.cboCategories.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.cboCategories);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ComboBox cboCategories;
}
}
JobCategories.xml (with the 'Copy to Output Directory' property set to 'Copy Always') -
<?xml version="1.0" encoding="Windows-1252"?>
<JobCategories>
<JobCategories>
<JobCatID>8</JobCatID>
<JobCategory>Accountancy & Finance</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>20</JobCatID>
<JobCategory>Banking, Financial services & Insurance</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>11</JobCatID>
<JobCategory>Beauty,Hair Care,Leisure & Sport</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>9</JobCatID>
<JobCategory>Construction, Architecture & Property</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>23</JobCatID>
<JobCategory>Customer Service, Call Centres & Languages</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>13</JobCatID>
<JobCategory>Education, Childcare & Training</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>6</JobCatID>
<JobCategory>Engineering & Utilities</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>2</JobCatID>
<JobCategory>Environmental, Health & Safety</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>27</JobCatID>
<JobCategory>General Management</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>1</JobCatID>
<JobCategory>Hotel & Catering</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>16</JobCatID>
<JobCategory>HR & Recruitment</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>3</JobCatID>
<JobCategory>IT</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>4</JobCatID>
<JobCategory>Legal</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>21</JobCatID>
<JobCategory>Marketing</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>17</JobCatID>
<JobCategory>Nursing, Healthcare & Medical</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>7</JobCatID>
<JobCategory>Production, Manufacturing & Materials</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>115</JobCatID>
<JobCategory>Public Sector</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>25</JobCatID>
<JobCategory>Publishing, Media & Creative Arts</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>10</JobCatID>
<JobCategory>Retailing, Wholesaling & Purchasing</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>24</JobCatID>
<JobCategory>Sales</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>26</JobCatID>
<JobCategory>Science, Pharmaceutical & Food</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>114</JobCatID>
<JobCategory>Secretarial & Admin</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>14</JobCatID>
<JobCategory>Security, Trades & General Services</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>12</JobCatID>
<JobCategory>Social & Not for Profit</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>5</JobCatID>
<JobCategory>Telecoms</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>19</JobCatID>
<JobCategory>Tourism, Travel & Airlines</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>18</JobCatID>
<JobCategory>Transport, Warehousing & Motor</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>28</JobCatID>
<JobCategory>Graduate</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>29</JobCatID>
<JobCategory>Executive</JobCategory>
</JobCategories>
<JobCategories>
<JobCatID>116</JobCatID>
<JobCategory>Motoring</JobCategory>
</JobCategories>
</JobCategories>
Which produces the following output --saige-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Saige,
So I compared your Form1.Designer.cs with mine and came to the conclusion that there is nothing you added in here, its all generated code (correct?).
Next, I was concerned because my XML format was so different from yours. So I added these lines to check if my XML was being parsed into the DataSet correctly:
So those diagnostic lines showed me the XML was being parsed okay. So it was probably about display in the Combo Box. So I looked into those 2x combo box properties you were setting....
I added these two lines, for key and value, *after* assigning the DataSource:
This did the trick, it was more to do with setting the ComboBox than correcting anything to do with XML parsing (as I first thought)...
thanks!
So I compared your Form1.Designer.cs with mine and came to the conclusion that there is nothing you added in here, its all generated code (correct?).
Next, I was concerned because my XML format was so different from yours. So I added these lines to check if my XML was being parsed into the DataSet correctly:
String sColumnName = dsCategories.Tables[0].Columns[0].ColumnName;
sColumnName = dsCategories.Tables[0].Columns[1].ColumnName;
So those diagnostic lines showed me the XML was being parsed okay. So it was probably about display in the Combo Box. So I looked into those 2x combo box properties you were setting....
I added these two lines, for key and value, *after* assigning the DataSource:
cboCategories.DataSource = dsCategories.Tables[0];
cboCategories.ValueMember = dsCategories.Tables[0].Columns[0].ToString();
cboCategories.DisplayMember = dsCategories.Tables[0].Columns[1].ToString();
This did the trick, it was more to do with setting the ComboBox than correcting anything to do with XML parsing (as I first thought)...
thanks!
ASKER
Sorry, I didn't see your updated answer till now! (not the first time had problems with EE page updates...)
Ok, thanks, and the sort bit is useful too.
many thanks!!
Ok, thanks, and the sort bit is useful too.
many thanks!!
Form1.cs -
Open in new window
Form1.Designer.cs -Open in new window
DataFile.xml (with the 'Copy to Output Directory' property set to 'Copy Always')-Open in new window
Produces the following output --saige-