locked
Filling up Combobox through DisplayMember and ValueMember RRS feed

  • Question

  • I've got the entity class Seizoen

    [Table(Name = "seizoen")]
      public class Seizoen
      {
        [Column(IsPrimaryKey = true, IsDbGenerated=true)]
        public int idseizoen { get; set; }
        [Column]
        public String seizoen_naam { get; set; }
        [Column]
        public Decimal prijs_abo{ get; set; }
        [Column]
        public Decimal prijs_abo_red{ get; set; }
      
      }

    In the form I fill up the combobox with data from the SQL server

    var qryseizoenen = (from s in db.Seizoenen
    orderby s.seizoen_naam descending
    select s).ToList();
    
    this.seizoencomboBox.DataSource = qryseizoenen;
    this.seizoencomboBox.DisplayMember = "seizoen_naam";
    this.seizoencomboBox.ValueMember = "idseizoen";
    
    seizoencomboBox.SelectedItem = qryseizoenen.First();
    fillForm();
    
    

    But displaymember and Valuemember stay empty strings. Is that normal?

    Depending from which item is selected in the combobox some textbox are filled with data from the database.

     

    public void fillForm()
    {
    //here I get the error type Seizoen cannot be convert to int32
    //but valuemember has to be idseizoen which is already an int
    Int32 id = Convert.ToInt32(seizoencomboBox.SelectedValue);
    
    var qryprijzen = from s in db.Seizoenen
    where s.idseizoen == id
    select s;
    
    foreach (Seizoen seiz in qryprijzen)
    {
    prijsabotextBox.Text = seiz.prijs_abo.ToString();
    prijsaboredtextBox.Text = seiz.prijs_abo_red.ToString();
    }
    }

    I followed the steps on the msdn.

    What am I doing wrong to generate these errors?

     

    Friday, May 14, 2010 2:40 PM

Answers

  • Hi KwintenB,

    You can simply bind that two textboxes to the same datasource with the combobox, in this case you do not need to find the selected item again. For example:

    NorthwindDataContext db = new NorthwindDataContext ();
    var results = (from p in db.Customers             
           select p).ToList ();
    
    this.comboBox1.DataSource = results;
    this.comboBox1.DisplayMember = "CompanyName";
    this.comboBox1.ValueMember = "CustomerID";
    
    this.textBox1.DataBindings.Add("Text", results, "ContactName");// Bind textbox1 to column "ContactName"
    this.textBox2.DataBindings.Add("Text", results, "ContactTitle");// Bind textbox2 to column "ContactTitle"
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Alex Liang Friday, May 21, 2010 8:21 AM
    Monday, May 17, 2010 10:17 AM

All replies

  • I did some more debugging

    The program works when i do the folowing

    private void fillCombo()
        {
          var qryseizoenen = (from s in db.Seizoenen
                    orderby s.seizoen_naam descending
                    select s).ToList();
    
          this.seizoencomboBox.DataSource = qryseizoenen;
          this.seizoencomboBox.DisplayMember = "seizoen_naam";
          this.seizoencomboBox.ValueMember = "idseizoen";
    
          seizoencomboBox.SelectedItem = qryseizoenen.First();
    
          Seizoen seiz = db.Seizoenen.Single<Seizoen>(s => s.idseizoen == Convert.ToInt32(seizoencomboBox.SelectedValue));
          prijsabotextBox.Text = seiz.prijs_abo.ToString();
          prijsaboredtextBox.Text = seiz.prijs_abo_red.ToString();
        }

    but when i replace the last block of code with fillForm(); which is exactly the same code, it doesn't work anymore

    private void fillForm()
        {
          Seizoen seiz = db.Seizoenen.Single<Seizoen>(s => s.idseizoen == Convert.ToInt32(seizoencomboBox.SelectedValue));
          prijsabotextBox.Text = seiz.prijs_abo.ToString();
          prijsaboredtextBox.Text = seiz.prijs_abo_red.ToString();
        }
    private void seizoencomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
          fillForm();
        }

    Also when I copy these 3 lines of code into this eventhandler the program stops working.

    This is so weird

    Friday, May 14, 2010 3:40 PM
  • Hi KwintenB,

    You can simply bind that two textboxes to the same datasource with the combobox, in this case you do not need to find the selected item again. For example:

    NorthwindDataContext db = new NorthwindDataContext ();
    var results = (from p in db.Customers             
           select p).ToList ();
    
    this.comboBox1.DataSource = results;
    this.comboBox1.DisplayMember = "CompanyName";
    this.comboBox1.ValueMember = "CustomerID";
    
    this.textBox1.DataBindings.Add("Text", results, "ContactName");// Bind textbox1 to column "ContactName"
    this.textBox2.DataBindings.Add("Text", results, "ContactTitle");// Bind textbox2 to column "ContactTitle"
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Alex Liang Friday, May 21, 2010 8:21 AM
    Monday, May 17, 2010 10:17 AM
  • This solution worked perfect.

    When i wanted to remove an item from the combobox i had to add this lines of code.

    this.comboBox.DataSource = null;
    this.comboBox.Items.Clear();
    this.textBox1.DataBindings.Clear();
    this.textBox2.DataBindings.Clear();

    I also have an other form with the same functionality. Selecting something from the combobox and filling the 7 textboxes of that form. To clear the data them it's easier to make a new query and clear them with a foreach loop.

    Friday, May 21, 2010 7:05 PM