Reputation: 25
I get the combobox to show the "Name", but when clicking on the name, the "type" and "living" does not want to show up in the textboxes.
Also, is there a way to get this to work without the <'data><'/data> in the xml?
The data.xml
<?xml version="1.0"?>
<Data>
<Start Name="Anaconda" type="Snake" living="Nowhere" />
<Start Name="Sphynx" type="Cat" living="Everywhere" />
<Start Name="Amanita muscaria" type="Fungus" living="Woodstock" />
</Data>
the C# code:
public Form1()
{
InitializeComponent();
DataSet dataSet = new DataSet();
dataSet.ReadXml("data.xml");
this.comboBox1.DataSource = dataSet.Tables[0];
this.comboBox1.DisplayMember = "Name";
}
Upvotes: 0
Views: 579
Reputation: 197
This is a headstart to have the values bound to some textboxes:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var dataSet = new DataSet();
var bindingSource = new BindingSource();
bindingSource.DataSource = dataSet.ReadXml("data.xml");
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = dataSet.Tables[0];
tbName.DataBindings.Add("Text", comboBox1.SelectedItem, "Name");
tbType.DataBindings.Add("Text", comboBox1.SelectedItem, "type");
tbLiving.DataBindings.Add("Text", comboBox1.SelectedItem, "living");
}
}
EDIT:
A complete example puts the read content of the XML file into a class and binds that to all controls.
Shoutouts to @Jimi for Binding a TextBox to a ListBox SelectedItem. This helped really much as orientation.
public partial class Form1 : Form
{
#region Private Members
/// <summary>
/// The content list to bind to.
/// </summary>
private BindingList<Data> mData = null;
/// <summary>
/// The item to bind to.
/// </summary>
private BindingSource mDataSource = null;
#endregion
#region Constructor
/// <summary>
/// Default constructor.
/// </summary>
public Form1()
{
InitializeComponent();
// Get binding content
mData = GetXmlData("data.xml");
// Prepare the binding source from the read content
mDataSource = new BindingSource(mData, null);
// Set what is to be displayed
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = mDataSource;
// Bind textboxes
tbName.DataBindings.Add(new Binding("Text", mDataSource, "Name", false, DataSourceUpdateMode.OnPropertyChanged));
tbType.DataBindings.Add(new Binding("Text", mDataSource, "type", false, DataSourceUpdateMode.OnPropertyChanged));
tbLiving.DataBindings.Add(new Binding("Text", mDataSource, "living", false, DataSourceUpdateMode.OnPropertyChanged));
}
#endregion
/// <summary>
/// Reads the provided XML file and puts it into a structured binding list.
/// </summary>
/// <returns></returns>
private BindingList<Data> GetXmlData(string xmlFile)
{
// Create a data set and read the file
var dataSet = new DataSet();
dataSet.ReadXml(xmlFile);
// Convert the content to a List<Data>
var data = dataSet.Tables[0].AsEnumerable().Select(r => new Data
{
Name = r.Field<string>("Name"),
Type = r.Field<string>("type"),
Living = r.Field<string>("living")
}).ToList();
// Return the content as BindingList<Data>
return new BindingList<Data>(data);
}
}
In this case you would need a class to put the content of the file into.
The Data
class is as follows:
/// <summary>
/// The structure of a read XML file.
/// </summary>
public class Data
{
/// <summary>
/// The name of an item.
/// </summary>
public string Name { get; set; }
/// <summary>
/// The type of an item.
/// </summary>
public string Type { get; set; }
/// <summary>
/// The living space of an item.
/// </summary>
public string Living { get; set; }
}
It was mentioned before that one would need a unique identifier to deal with double data. Here you don't. The combo box binds to objects of Data
and is unique to itself.
Upvotes: 1