Meilleur auteur de réponses
Comment récupérer le value d'une ligne checkedListBox ?

Question
-
Bonjour à tous,
Voici mon problème, je suis actuellement en train de développer une application permettant d'afficher dans une checkedLisBox l'ensemble de mes produits à louer. J'aimerais sur l'événement clic de la case à cocher, récupérer l'Id de ma ligne.
Pourriez-vous m'aider ?
code pour afficher les information dans ma checkedListBox ://Création de l'objet responsable de l'exécution des requêtes maCommand = factory.CreateCommand(); maCommand.CommandText = "select* from jos_mleweb_location"; maConnexion.Open(); maCommand.Connection = maConnexion; //Méthode qui ExecuteReader monLecteur = maCommand.ExecuteReader(); int i = 0; while (monLecteur.Read()) { int num = (int)monLecteur["loc_id"]; string reference = (string)monLecteur["loc_reference"]; string nomProduit = (string)monLecteur["loc_name"]; int prix = (int)monLecteur["loc_price"]; checkedListBoxProduit.Items.Add("Référence : "+reference+" - Nom du produit : "+nomProduit+" - Prix : "+prix+"€"); i++; }
Fonction qui permet de récupérer l'id de ma ligne :
private void checkedListBoxProduit_SelectedIndexChanged(object sender, EventArgs e) { string reference = (string)checkedListBoxProduit.SelectedItem; int idProduit = (int)checkedListBoxProduit.SelectedValue; MessageBox.Show("la référence est : "+reference+"- Numero du produit"+idProduit); }
- Modifié Mado60 mardi 8 mai 2012 12:26
Réponses
-
Ben j'ai une première idée toute simple : Tu ajoutes cette classe :
public class location { public int ID; public string Text; public string Reference; public override string ToString() { return this.Text; } }
tu modifies ta boucle :
location loca; while (monLecteur.Read()) { int num = (int)monLecteur["loc_id"]; string reference = (string)monLecteur["loc_reference"]; string nomProduit = (string)monLecteur["loc_name"]; int prix = (int)monLecteur["loc_price"]; loca = new location(); loca.ID = num; loca.Reference=reference; loca.Text = "Référence : "+reference+" - Nom du produit : "+nomProduit+" - Prix : "+prix+"€"); checkedListBoxProduit.Items.Add(loca); i++; }
et dans SelectedIndexChanged :
string reference =((location)(checkedListBoxProduit.SelectedItem)).Reference.ToString(); int idProduit = ((location)(checkedListBoxProduit.SelectedItem)).ID; MessageBox.Show("la référence est : "+reference+"- Numero du produit"+idProduit);
- Modifié Christophe PeuMVP mardi 8 mai 2012 13:08
- Proposé comme réponse Christophe PeuMVP mardi 8 mai 2012 13:34
- Marqué comme réponse Ciprian Duduiala mardi 8 mai 2012 13:58
Toutes les réponses
-
-
-
Ben j'ai une première idée toute simple : Tu ajoutes cette classe :
public class location { public int ID; public string Text; public string Reference; public override string ToString() { return this.Text; } }
tu modifies ta boucle :
location loca; while (monLecteur.Read()) { int num = (int)monLecteur["loc_id"]; string reference = (string)monLecteur["loc_reference"]; string nomProduit = (string)monLecteur["loc_name"]; int prix = (int)monLecteur["loc_price"]; loca = new location(); loca.ID = num; loca.Reference=reference; loca.Text = "Référence : "+reference+" - Nom du produit : "+nomProduit+" - Prix : "+prix+"€"); checkedListBoxProduit.Items.Add(loca); i++; }
et dans SelectedIndexChanged :
string reference =((location)(checkedListBoxProduit.SelectedItem)).Reference.ToString(); int idProduit = ((location)(checkedListBoxProduit.SelectedItem)).ID; MessageBox.Show("la référence est : "+reference+"- Numero du produit"+idProduit);
- Modifié Christophe PeuMVP mardi 8 mai 2012 13:08
- Proposé comme réponse Christophe PeuMVP mardi 8 mai 2012 13:34
- Marqué comme réponse Ciprian Duduiala mardi 8 mai 2012 13:58
-
Je vous remercie, cela fonctionne très très bien même.
Au cas ou si ce code peut intéresser quelqu'un d'autre, il y a juste une parenthèse à enlever à la fin de cette ligne :
loca.Text = "Référence : "+reference+" - Nom du produit : "+nomProduit+" - Prix : "+prix+"€");
Encore merci d'avoir pris le temps de me répondre ;) Bonne journée à vous