var monBinding = (from conso in Commande.Liste select new { Nom = conso.Produit.Nom, Quantite = conso.Quantite, Numero = Commande.Numero, //etc.. }).toList();
public string NomProduit {get; private set;}
ublic void Add(Produit produit) { int index = this.IndexOf(produit); if (index == -1) { consommation = new Consommation(produit, 1); this.ListeConsommations.Add(consommation); NomProduit = produit.Nom; } else { this.ListeConsommations[index].quantite++; } }
: le datagridview se met bien à jour lorsqu'un nouvel élément est ajouté à la liste. mais pas lorsque la quantité change. Pourtant le onPropertyChanged est sur mes méthodes Add et Remove et donc l'évènement devrait être levé dans les deux cas.
Deuxième problème : j'aimerais bien trier tout abord mes colonnes. Par défaut il prend l'ordre dans lequel les accesseurs sont définis dans ma classe. Est-ce qu'il y a une autre manière de faire.
j'aimerai bien qu'il ne m'affiche pas mon objetheu pas compris, quel objet?
ajouter une colonne à mon datagridview avec un bouton., je ne sais pas comme ça, jamais essayé et je dois repartir.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionpublic class Commande { private BindingList<Consommation> listeConsommations; private Consommation consommation; public Commande() { listeConsommations = new BindingList<Consommation>(); } public BindingList<Consommation> ListeConsommations { get { return listeConsommations; } } private int IndexOf(Produit Produit) { //Je me suis permis de remplacer ta boucle par une requete Linq, ça n'est pas plus simple, juste pour l'exemple if (listeConsommations.Any(c => c.NomProduit == Produit.Nom)) return listeConsommations.Select((c, i) => new { Nom = c.NomProduit, Index = i }).Single(x => x.Nom == Produit.Nom).Index; else return -1; } public void Add(Produit Produit) { int index = this.IndexOf(Produit); if (index == -1) { consommation = new Consommation(Produit, 1); this.ListeConsommations.Add(consommation); } else { this.ListeConsommations[index].Quantite++; } } public void Remove(Produit Produit) { int index = this.IndexOf(Produit); if (index != -1) { if (this.ListeConsommations[index].Quantite == 1) { this.ListeConsommations.Remove(this.ListeConsommations[index]); } else { this.ListeConsommations[index].Quantite--; } } } } public class Consommation:INotifyPropertyChanged { public Consommation(Produit P, int Quantite) { ProduitCommande = P; quantite = Quantite; NomProduit = P.Nom; RisePropertyChanged("NomProduit"); } private int quantite; public int Quantite { get { return quantite; } set { if (quantite != value) { quantite = value; RisePropertyChanged("Quantite"); } } } private Produit produit; public Produit ProduitCommande { get { return produit; } set { if (produit != value) { produit = value; RisePropertyChanged("ProduitCommande"); } } } public string NomProduit { get; private set; } #region INotifyPropertyChanged Membres public event PropertyChangedEventHandler PropertyChanged; private void RisePropertyChanged(string NomProp) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(NomProp)); } #endregion } public class Produit { public string Nom { get; set; } }
Produit the = new Produit { Nom = "Thé" }; Produit cafe = new Produit { Nom = "Café" }; Produit biere = new Produit { Nom = "Bière" }; mesProduits.Add(the); mesProduits.Add(cafe); mesProduits.Add(biere); //charge une commande en cours for (int i = 0; i < 3; i++) { maCommande.Add(the); maCommande.Add(cafe); maCommande.Add(biere); } consommationBindingSource.DataSource = maCommande.ListeConsommations;
int indexProduit = 0; Commande maCommande = new Commande(); List<Produit> mesProduits = new List<Produit>(); private void testThomic() { maCommande.Remove(mesProduits[indexProduit++]); if (indexProduit == 3) indexProduit = 0; }
private void Consommation_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
chargementProduits();
afficherSofts();
var monBinding = (from conso in commande.listeConsommations
select new
{
Nom = conso.produitCommande.nom,
Quantite = conso.quantite
}).ToList();
dataGridView1.DataSource = monBinding;
btSoft_Click(sender, e);//on simule le clic sur le bouton bière
}
commande.Add(produit);
public void Add(Produit produit)
{
int index = this.IndexOf(produit);
if (index == -1)
{
consommation = new Consommation(produit, 1);
this.ListeConsommations.Add(consommation);
}
else
{
this.ListeConsommations[index].quantite++;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
namespace GestionBar
{
class Commande : INotifyPropertyChanged
{
private BindingList<Consommation> ListeConsommations;
private Consommation consommation;
public Commande()
{
ListeConsommations = new BindingList<Consommation>();
}
public BindingList<Consommation> listeConsommations
{
get { return ListeConsommations; }
}
private int IndexOf(Produit produit)
{
int index = -1;
for (int i = 0; i < ListeConsommations.Count; i++)
{
if (ListeConsommations[i].produitCommande == produit)
{
index = i;
break;
}
}
return index;
}
public void Add(Produit produit)
{
int index = this.IndexOf(produit);
if (index == -1)
{
consommation = new Consommation(produit, 1);
this.ListeConsommations.Add(consommation);
}
else
{
this.ListeConsommations[index].quantite++;
}
this.OnPropertyChanged("listeConsommations");
}
public void Remove(Produit produit)
{
int index = this.IndexOf(produit);
if (index != -1)
{
if (this.ListeConsommations[index].quantite == 1)
{
this.ListeConsommations.Remove(this.ListeConsommations[index]);
}
else
{
this.ListeConsommations[index].quantite--;
}
this.OnPropertyChanged("listeConsommations");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
private void Consommation_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
chargementProduits();
afficherSofts();
commandeBindingSource.DataSource = commande.listeConsommations;
dataGridView1.DataSource = commandeBindingSource;
}
BindingSource commandeBindingSource = new BindingSource();
commandeBindingSource.DataSource = commande.ListeConsommations;
dataGridView1.DataSource = commandeBindingSource;
List<Consommation> ListeTriee = ListeConsommations.OrderBy(x => x.nomProduit).ToList();
this.ListeConsommations = new BindingList<Consommation>(ListeTriee);
ListeConsommations = ListeConsommations.OrderBy(x => x.nomProduit).ToList();