locked
como llenar ComboBox desde una base de datos, que no sea en el load de mi formulario C# RRS feed

  • Question

  • como llenar ComboBox desde una base de datos, que no sea en el load de mi formulario C#

    tengo esa pequeña que cuando quiero pasarle el valor a mi Combobox desde mi datagrid se me carga automaticamente el valor con el cual llene el combobox no con el valor que registre el usuario.

    • Moved by Eyal Solnik Sunday, May 29, 2016 7:42 AM foreign language
    Wednesday, January 6, 2016 4:30 PM

Answers

  • Define a class

    public class Language
    {
         public string Name { get; set; }
         public string Value { get; set; }
    }

    then...

    //Build a list
    var dataSource = new List<Language>();
    dataSource.Add(new Language() { Name = "blah", Value = "blah" });
    dataSource.Add(new Language() { Name = "blah", Value = "blah" });
    dataSource.Add(new Language() { Name = "blah", Value = "blah" });
    
    //Setup data binding
    this.comboBox1.DataSource = dataSource;
    this.comboBox1.DisplayMember = "Name";
    this.comboBox1.ValueMember = "Value";
    
    // make it readonly
    this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;


    If the answer was helpful vote

    • Proposed as answer by Jean LLopes Thursday, January 7, 2016 12:27 AM
    • Marked as answer by Kristin Xie Monday, January 18, 2016 9:50 AM
    Thursday, January 7, 2016 12:27 AM