Problème de récupération de value d'une ligne avec un checkedListBox
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 :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| //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 :
Code:
1 2 3 4 5 6
| 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);
} |
Merci d'avance de votre aide :)
Edit
Au cas ou si cela peut aider quelqu'un, le problème a été trouvé, je vous fait par ce cette trouvaille :
Code:
1 2 3 4 5 6 7 8 9 10 11
| public class location
{
public int ID;
public string Text;
public string Reference;
public override string ToString()
{
return this.Text;
}
} |
tu modifies ta boucle :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 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 :
Code:
1 2 3
| 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); |