【问题标题】:Fill textboxes with data from a combobox, connected with xml用来自组合框的数据填充文本框,与 xml 连接
【发布时间】:2019-10-24 17:47:03
【问题描述】:

我让组合框显示“名称”,但是当单击名称时,“类型”和“生活”不想出现在文本框中。

另外,有没有办法让它在 xml 中没有 '/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";
    }

【问题讨论】:

  • 您的问题描述不清楚,但如果您正在获取名称、类型和居住在文本框中,那是因为您将整行作为组合框的输入并试图在文本框中获取它。尝试从 dataSet.Tables[0] 中获取名称,因为它包含所有三个。
  • 使用 TextBoxes DataBindings 属性。添加一个新的Binding,使用您的DataSource 将Text 属性绑定到DataColumn 的值。见那里的例子。在中间添加BindingSource 可能会改善体验。
  • @Shilpa,我没有得到关于类型或生活在文本框中的价值。更改组合框时,我什至无法在文本框中显示任何数据。
  • @Jimi,我试图让绑定和数据列工作几个小时。我只是无法让它工作,我试过了:comboBox1.DataBindings.Add(new Binding("Text", dataSet, "dataSet.Name")); textBox1.DataBindings.Add(new Binding("Text", dataSet, "dataSet.type"));仍然没有显示。你能帮忙提供代码和解释吗,我对 docs.microsoft 感到困惑。
  • @stackoverflowknitter 你在组合框中获取数据吗?如果是,请告诉您如何将组合框值带到文本框。正如你只需要说 textbox1.Text=comboBox1.SelectedItem;

标签: c# winforms combobox textbox


【解决方案1】:

这是将值绑定到某些文本框的先机:

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 文件内容放入一个类中,并将其绑定到所有控件。

向@Jimi 致敬Binding a TextBox to a ListBox SelectedItem。 这对定位很有帮助。

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);
    }
}

在这种情况下,您需要一个类来将文件的内容放入其中。 Data 类如下:

/// <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; }
}

之前提到过需要一个唯一标识符来处理双重数据。在这里你没有。组合框绑定到Data 的对象,并且对它自己是唯一的。

【讨论】:

  • 有了这个我更进一步。我现在得到文本框中的第一个值。但我仍然无法弄清楚如何获得改变组合框中项目的值来改变。同样如 Demo... 所写,如果它的名字完全一样,它会出错吗?如果是这样,将它作为一个类导入是否更容易?
  • 我建议创建一个事件来更新文本框数据绑定。你不需要上课就可以让它工作。是否需要一个类来读取和存储值取决于您的结构。
  • 我这两天试了,完全搞不明白。我确实尝试将 MoveNext() 也与数据绑定一起使用。添加“这个”。并在组合框中进行更改以进行更新,但我只得到文本框中的第一行。我猜这就像绑定我想要的文本框链接一样,试图给它发短信但得到错误甚至试图复制和粘贴。可能是我遗漏了一些插件还是我做错了?
  • @stackoverflowknitter 你有没有想出另一个解决方案?
猜你喜欢
  • 1970-01-01
  • 2014-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-23
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多