databinding to a combobox

M

Matthijs de Z

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
 
M

Matthijs de Z

[...]
What must be done in order to get the values of myList in the combobox?

Use BindingList<T> instead of List<T>. The latter doesn't provide any
"data changed" events for the ComboBox to watch, but the former does.

hi pete,

bindinglist did the trick.
Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top