O soluție mai bună (în funcție de mărimea sursei de date) este de a declara două obiecte BindingSource
(noi începând cu 2.00) să lege colecția cu cele care apoi le leagă pe comboboxuri.
Eu anexez un exemplu complet.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private BindingSource source1 = new BindingSource();
private BindingSource source2 = new BindingSource();
public Form1()
{
InitializeComponent();
Load += new EventHandler(Form1Load);
}
void Form1Load(object sender, EventArgs e)
{
List myitems = new List
{
"Item 1",
"Item 2",
"Item 3"
};
ComboBox box = new ComboBox();
box.Bounds = new Rectangle(10, 10, 100, 50);
source1.DataSource = myitems;
box.DataSource = source1;
ComboBox box2 = new ComboBox();
box2.Bounds = new Rectangle(10, 80, 100, 50);
source2.DataSource = myitems;
box2.DataSource = source2;
Controls.Add(box);
Controls.Add(box2);
}
}
}
Dacă doriți să vă confundați chiar mai mult, încercați întotdeauna declanșarea legăturilor în constructor. Asta poate duce la niște bug-uri curioase într-adevăr , de aceea întotdeauna se leagă în evenimentul Load.