Faire un tri sur un dataset d'après la selection dans une combobox
Bonjour,
Je début en c#, j'essaie de créer une petite appli de gestion de congélateur. Je me connecte sans problème à ma BDD sql puis je récupère les données dans un dataset.
Mon problème vient de mon dataview, lorsque je modifie la ligne selectionnée dans ma combobox les lignes s'ajoute dans ma listbox sans remise à 0. Je ne comprends pas pourquoi ni comment je dois faire pour n'avoir qu'une ligne de chaque enregistrement.
Merci d'avance pour votre aide.
Mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Npgsql;
namespace congelateur
{
public partial class frmCongelateur : Form
{
private DataSet ds = new DataSet();
private DataTable dt = new DataTable();
public frmCongelateur()
{
InitializeComponent();
changementEtat(false);
grpUnite.Visible = false;
ds = recupInfos("categories", "SELECT numcategorie, libelle FROM categories");
cboCatégorie.DataSource = ds.Tables["categories"];
cboCatégorie.ValueMember = "numcategorie";
cboCatégorie.DisplayMember = "libelle";
}
private DataSet recupInfos(string nomTable,string sql)
{
try
{
// PostgeSQL-style connection string
string connstring = String.Format("Server=*******;Port=****;" +
"User Id=******;Password=*******;Database=CONGELATEUR;");
// Making connection with Npgsql provider
NpgsqlConnection conn = new NpgsqlConnection(connstring);
conn.Open();
// data adapter making request from our connection
NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
// filling DataSet with result from NpgsqlDataAdapter
da.Fill(ds,nomTable);
// since we only showing the result we don't need connection anymore
conn.Close();
return ds;
}
catch (Exception msg)
{
// something went wrong, and you wanna know why
MessageBox.Show(msg.ToString());
throw;
}
}
private void changementEtat(bool etat)
{
txtLibelle.Enabled = etat;
cboModifCat.Enabled = etat;
txtQuantite.Enabled = etat;
dtpDateCongel.Enabled = etat;
}
private void nouvelleVue(DataSet ds)
{
int categorie = cboCatégorie.SelectedIndex;
if (categorie < 0){
categorie = 0;
}
dv = new DataView(ds.Tables["produits"], "numcategorie=" + categorie.ToString(), "datecongelation desc", DataViewRowState.CurrentRows);
lstProduit.BeginUpdate();
lstProduit.DataSource = dv;
lstProduit.DisplayMember = "ligneproduit";
lstProduit.ValueMember = "numcategorie";
lstProduit.EndUpdate();
}
private void cboCatégorie_SelectedIndexChanged(object sender, EventArgs e)
{
ds = recupInfos("produits", "SELECT *, concat(datecongelation, ' ', nom, ' ', quantite, ' ', typeunite) as ligneproduit FROM produits inner join unite using (numunite)");
nouvelleVue(ds);
}
}
} |