用来自组合框的数据填充文本框,连接 xml
Fill textboxes with data from a combobox, connected with xml
我得到了显示 "Name" 的组合框,但是当单击名称时,"type" 和 "living" 不想显示在文本框中。
此外,有没有办法在 xml 中没有 <'data><'/data> 的情况下使它工作?
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>
C#代码:
public Form1()
{
InitializeComponent();
DataSet dataSet = new DataSet();
dataSet.ReadXml("data.xml");
this.comboBox1.DataSource = dataSet.Tables[0];
this.comboBox1.DisplayMember = "Name";
}
这是将值绑定到某些文本框的开端:
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");
}
}
编辑:
一个完整的例子是将 XML 文件的读取内容放入 class 并将其绑定到所有控件。
感谢@Jimi 。
这对定位很有帮助。
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);
}
}
在这种情况下,您需要一个 class 来放入文件的内容。
Data
class如下:
/// <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; }
}
之前提到过,处理double数据需要一个唯一标识。在这里你没有。组合框绑定到 Data
的对象并且是唯一的。
我得到了显示 "Name" 的组合框,但是当单击名称时,"type" 和 "living" 不想显示在文本框中。
此外,有没有办法在 xml 中没有 <'data><'/data> 的情况下使它工作?
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>
C#代码:
public Form1()
{
InitializeComponent();
DataSet dataSet = new DataSet();
dataSet.ReadXml("data.xml");
this.comboBox1.DataSource = dataSet.Tables[0];
this.comboBox1.DisplayMember = "Name";
}
这是将值绑定到某些文本框的开端:
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");
}
}
编辑:
一个完整的例子是将 XML 文件的读取内容放入 class 并将其绑定到所有控件。
感谢@Jimi
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);
}
}
在这种情况下,您需要一个 class 来放入文件的内容。
Data
class如下:
/// <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; }
}
之前提到过,处理double数据需要一个唯一标识。在这里你没有。组合框绑定到 Data
的对象并且是唯一的。