0

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";
    }
  • Your problem description is not clear but if you are getting name, type and living in textbox thats because you are giving whole row as a input to combobox and trying to get it in textbox. Try to get name out of dataSet.Tables[0] as it has all three in it. – Shilpa Oct 24 '19 at 18:53
  • Use the TextBoxes [DataBindings](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.databindings) property. Add a new `Binding` using your DataSource to bind the Text property to a `DataColumn`'s value. See the example there. Adding a [BindingSource](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.bindingsource) in-between may improve the experience. – Jimi Oct 24 '19 at 22:25
  • @Shilpa, I am not getting the value on type or living in the textboxes. I cant even get any data to show up in the textboxes when changing the combobox. – stackoverflowknitter Oct 25 '19 at 06:49
  • @Jimi, I have tried to get binding and datacolumn to work for hours. I just can't get it to work, I tried: comboBox1.DataBindings.Add(new Binding("Text", dataSet, "dataSet.Name")); textBox1.DataBindings.Add(new Binding("Text", dataSet, "dataSet.type")); still not showing. Can you help with code and explanation, I get confused with docs.microsoft. – stackoverflowknitter Oct 25 '19 at 06:49
  • @stackoverflowknitter are you getting data in combobox? if yes please tell how you are taking combobox value to textbox. As you just have to say textbox1.Text=comboBox1.SelectedItem; – Shilpa Oct 25 '19 at 10:15
  • @Shilpa, I am not getting any data to the textbox1 execpt only the name that is selected in Combobox1 (that I do not want to show in textbox1). Its just the code I have manage to create. – stackoverflowknitter Oct 25 '19 at 10:24

1 Answers1

1

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.

Faenrig
  • 197
  • 1
  • 9
  • with this I get a step further. I now get the first value in the textboxes. But I can still not figure out how to get the value to change wen changing the items in combobox. Also as Demo...wrote, if its same name in all, will it get error, and if so, is it easier to import it as a class instead? – stackoverflowknitter Oct 25 '19 at 10:21
  • I'd suggest creating an event to update the textbox databinding. You don't need a class yet to get this to work. It depends on your structure whether you need a class to read and store values or not. – Faenrig Oct 25 '19 at 10:57
  • @stackoverflowknitter [Binding a TextBox to a ListBox SelectedItem](https://stackoverflow.com/a/57235083/7444103) – Jimi Oct 25 '19 at 12:26
  • I have tried in two days now, I can't figure it out at all. I did try to use MoveNext() also with databinding. Adding "this." And changing in the combobox to update but I only get the first line in textbox. I guessed it's something like that binding a textbox-link I want, tried to text it but getting error even tried to copy and paste. Might I be missing some plugins or am I doing it wrong? – stackoverflowknitter Oct 28 '19 at 05:48
  • @stackoverflowknitter Did you come up with another solution though? – Faenrig Oct 29 '19 at 06:57
  • @Faenrig. Now It works! All input will be unique. I don't really understand everything in the script, but I understand the most of it. – stackoverflowknitter Oct 30 '19 at 11:34
  • @stackoverflowknitter Alright, that's good to hear. Can you mark my answer then to close this down? Best thing you can do now is to break things apart to practice how everything works (e.g. changing the parameters of Binding or something). Just play around a little and observe what happens if you change certain things. – Faenrig Oct 31 '19 at 10:35