0

I am trying to get to grips with using DataSource with a combo. At the moment I have this:

private void EditMaterialsForm_Load(object sender, EventArgs e)
{
    try
    {
        cbMaterialGroups.DataSource = _docMaterialsDB.MaterialGroup;
        cbMaterialGroups.DisplayMember = "Name";
        //foreach(CutToolsDatabaseMaterialGroup oMaterialGroup in _docMaterialsDB.MaterialGroup)
        //{
        //    cbMaterialGroups.Items.Add(oMaterialGroup.Name);
        //}
    }
    catch (System.IO.FileNotFoundException /*e*/)
    {
        // TODO
        this.BeginInvoke(new MethodInvoker(this.Close));
    }

    if (cbMaterialGroups.Items.Count > 0)
        cbMaterialGroups.SelectedIndex = 0;

    SetControlStates();
}

But it does not display the data as I expect:

enter image description here

If I comment out my binding attempt and then trying the original foreach loop I get what I expect:

enter image description here

I tried setting:

cbMaterialGroupsValueMember = "Name";

But then it raises an exception.

How do I get it to display the correct value? I am missing something.

Update

I do have the name property in the class:

public class CutToolsDatabaseMaterialGroup
{
    [XmlElement]
    public string Name;

    [XmlElement(ElementName = "Material")]
    public List<CutToolsDatabaseMaterial> Materials;

    public CutToolsDatabaseMaterialGroup()
    {
        Name = "";
        Materials = new List<CutToolsDatabaseMaterial>();
    }
}

And:

public class CutToolsDatabaseMaterial
{
    [XmlElement]
    public string Name;

    [XmlArray]
    [XmlArrayItem(ElementName ="Value")]
    public List<string> Text;

    public CutToolsDatabaseMaterial()
    {
        Name = "";
        Text = new List<string>();
    }
}

Update

I tried the linked answer and used:

   public partial class EditMaterialsForm : Form
    {
        private MaterialsDatabase.CutToolsDatabase _docMaterialsDB;
        private BindingSource bindingMaterialGroups = new BindingSource();

        public EditMaterialsForm()
        {
            InitializeComponent();
        }

        private void EditMaterialsForm_Load(object sender, EventArgs e)
        {
            try
            {
                bindingMaterialGroups.DataSource = _docMaterialsDB.MaterialGroup;

                cbMaterialGroups.DataSource = bindingMaterialGroups.DataSource;
                cbMaterialGroups.DisplayMember = "Name";
                cbMaterialGroups.ValueMember = "Name";
                //foreach(CutToolsDatabaseMaterialGroup oMaterialGroup in _docMaterialsDB.MaterialGroup)
                //{
                //   cbMaterialGroups.Items.Add(oMaterialGroup.Name);
                //}
            }
            catch (System.IO.FileNotFoundException /*e*/)
            {
                // TODO
                this.BeginInvoke(new MethodInvoker(this.Close));
            }

            if (cbMaterialGroups.Items.Count > 0)
                cbMaterialGroups.SelectedIndex = 0;

            SetControlStates();
        }

But I still get an exception:

enter image description here

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • I didn't even try bindings in winforms myself, but from accepted answer in duplicate it seems you are missing `BindingSource`. – Sinatr Oct 01 '20 at 07:42
  • 1
    Its worth to simply google for error message. See [this](https://stackoverflow.com/a/25761826/1997232). You have to use *properties* for bindings, make `Name` a property. – Sinatr Oct 01 '20 at 08:16
  • @Sinatr I saw that. Other comments said that that answer was not right so I did not try. But I have just tried it and it is OK. Thanks. :) – Andrew Truckle Oct 01 '20 at 08:17
  • Wrong comments can't be downvoted you have to always take them with certain criticism. Also often people do *other* mistake which makes answer not working for them. – Sinatr Oct 01 '20 at 08:20

1 Answers1

2

Make sure you have Name property inside your class or your data source.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
Ravi V
  • 91
  • 8