Remplir un comboBox avec un arrayList

Résolu
francesdereve Messages postés 75 Date d'inscription mardi 6 décembre 2005 Statut Membre Dernière intervention 6 août 2007 - 11 janv. 2006 à 21:34
ambts Messages postés 4 Date d'inscription mardi 19 octobre 2010 Statut Membre Dernière intervention 8 avril 2011 - 30 avril 2010 à 00:12
J'ai eassayé les 3 solutions proposées ca ne marche pas
1ere solution: foreach(string str
in p
) this
.comboBox1
.Items
.Add(str
);
2 eme sol: ArrayList p=s.recuperernom();
for(int i=0;i<=p.count;i++){
comboBox1.Items.Add(p[i]);
3 eme sol:


this.comboBox.DataSource = myArrayList;

//voici la methode qui recherche la liste des codes dans la base
public static ArrayList listcode(){
ArrayList l=
new ArrayList();
OdbcConnection con=
new OdbcConnection("DSN=source2");
con.Open();

string req="select code from personne;";
OdbcCommand com=
new OdbcCommand(req,con);

//com.Connection=con;
OdbcDataReader r=com.ExecuteReader();

while(r.Read())
{

string p =r.GetString(4);
l.Add(p);
}

return l;
}



//methode qui devrait afficher la liste des codes dans le combo a chaque clic
private void comboBox1_Click(object sender, System.EventArgs e){
try{
ArrayList p=Classverifier.listcode();
foreach(string s in p){
this.comboBox1.Items.Add(s);}
}catch(Exception ex){MessageBox.Show(ex.Message);}
}

Mais quand j'essaie avec les 3 solutions proposées ca me donne le message Index was outside the bounds of the array
FD

FD

5 réponses

Mithrandir51 Messages postés 12 Date d'inscription mardi 3 janvier 2006 Statut Membre Dernière intervention 3 février 2006
12 janv. 2006 à 17:52
As-tu absolument besoin de ton arrayList car tu peux envoyer a ta méthode ton objet combobox qui se rempliera automatiquement

public listcode(ComboBox cmbCodes){//Passge du comboBox en parametre
OdbcConnection con=new OdbcConnection("DSN=source2");
con.Open();
string req="select code from personne;";
OdbcCommand com=new OdbcCommand(req,con);
OdbcDataReader r=com.ExecuteReader();
while(r.Read())
{
cmbCodes.Items.Add(r.GetString(4)); //Ici tu remplis ton comboBox
}

ensuite tu appeles ta methode comme ça

Classverifier.listcode(comboBox1);
3
Rejoignez-nous