Home Bind list from class to combobox
Reply: 1

Bind list from class to combobox

Tom de Beer
1#
Tom de Beer Published in 2018-01-11 12:31:23Z

I'm currently writing a pretty small program in C# and have this list that I want to bind to a combobox. Now, I've put that list in a class, and want to bind that list to a combobox. The code below shows how far I've come so far:

Form

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Locaties locaties = new Locaties();
        List<string> listofLocaties = locaties.retrieveLocations();

        cboxLocToevoegen.DataSource = ???;
        cboxLocOverzicht.DataSource = ???;

    }
}

Class

class Locaties
{
    public List<string> retrieveLocations()
    {
        List<string> LocatieList = new List<string>();
        LocatieList.Add("Koelkast");
        LocatieList.Add("Keukenlade");
        LocatieList.Add("Voorraadruimte");
        LocatieList.Add("Overige");

        return LocatieList;
    }
}

Now, I'm gonna be honest with you: my knowledge and experience with classes and methods is not perfect. That's why the solution might probably be simpler than I think. Please don't judge me on that, I'm still learning!

Anyway, I hope anyone can help me out with this!

Lorenzo Canavaggio
2#
Lorenzo Canavaggio Reply to 2018-01-11 12:46:55Z

Simply

 cboxLocToevoegen.DataSource = listofLocaties ;

or directly

 cboxLocToevoegen.DataSource = locaties.retrieveLocations();

you can also bind directly to a list of Locaties and then choose the property to display in the CB :

List<Locaties> listofLocaties = new List<Locaties>();
...
//Populate the list
...
cboxLocToevoegen.DataSource = listofLocaties ;
cboxLocToevoegen.DisplayMember = [a property of Locaties class];
// and the value of the CB could be another property of Locaties class:
cboxLocToevoegen.ValueMember = [the value property of Locaties class];

But ofc you have to write a new Locaties class :)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303631 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO