How to populate combobox from list of objects

Viewed 817

Good evening,

normally my problem should be solved with this C# Using a list of objects to populate combobox and keeping object properties accessible

but I don't get what I'm doing wrong here.

I have a solution in Visual Studio 2019 with 2 projects. One Project is for GUI and stuff, the other for the backend engine.

In the Engine-Project (Namespace is Engine) I have the following classes:

Clan

namespace Engine
{
  public class Clan : CharacterCreationStats
  {
    public int ID { get; set; }
    public string Name { get; set; }

    public Clan(int id, string name, int Val1, int Val2,) :base(Val1, Val2)
    {
      ID = id;
      Name = name;
    }
  }
}

Val1 and Val2 are used in more classes than "Clan", therefore I created a parentclass called "CharacterCreationStats". Public class with public declarations of int's like this:

namespace Engine
{
  public class CharacterCreationStats
  {
    public int Val1{ get; set; }
  }
}

And I have a class "Data" which contains all generic logic

Data

namespace Engine
{
  public static class Data
  {
    public static List<Clan> Clans = new List<Clan>();

    public const int CLAN_ID_NAME_A = 1;
    public const int CLAN_ID_NAME_B = 2;
     
    static Data()
    {
     PopulateClans();
    }

    private static void PopulateClans()
    {
     Clans.Add(new Clan(CLAN_ID_NAME_A,"NameA", 1, 2));
     Clans.Add(new Clan(CLAN_ID_NAME_B, "NameB", 1, 2));    
        }
  }
}

Everythings fine, list is filled. Now I want to bind a combobox to use these values (showing me the Names declared in "Name = name;" in the Clan-class).

For this I go into my second project, the GUI project where I have my form (no WPF) and I do this:

using Engine;

public partial class CreateClass: Form
{
        public CreateClass()
        {
            InitializeComponent();
        }
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
  cmbBoxClans.DataSource = Data.Clans;
  cmbBoxClans.ValueMember = null;
  cmbBoxClans.DisplayMember = "Name";
}

it doesn't show me anything in the dropdown menue of my combobox.

What did I get wrong?

I don't specifically need a solution to my problem rather a hint what I fail to see.

1 Answers

put these lines of code in the load event

        private void CreateClass_Load(object sender, EventArgs e)
        {
            cmbBoxClans.DataSource = Data.Clans;
            cmbBoxClans.ValueMember = null;
            cmbBoxClans.DisplayMember = "Name";

        }

Related