5
Answers

databinding to a combobox

Ask a question
Matthijs

Matthijs

15y
6.3k
1

Hi,

I'm trying to create a c# form where the combobox is bind to a list and if there's something added or removed from the list, the combobox will be up to date again. But I cannot get it to work.

this is what I have:
============

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
   public partial class Form1 : Form
   {
        private List<string> myList = new List<string>();
        int i =1 ;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myList.Add("line " + i);
            i++;
        }

        private void comboBox1_DataSourceChanged(object sender, EventArgs e)
        {
            comboBox1.DataSource = myList;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DataSourceChanged += new EventHandler(comboBox1_DataSourceChanged);
            comboBox1.DataSource = myList;
        }
    }
}

================
What must be done in order to get the values of myList in the combobox?

regards,

 

Matthijs


Answers (5)