j'aimerai ça calculer la somme des ventes par province car j'ai trois tables dans ma base des données et j'aimerai ça calculer la somme des ventes pour chaque province donc j'ai 3 comboboxs que je selectionne l'année de début et de fin ensuite un autre combobox pour le genre du vehicule j'aimerai ça en fonction des valeurs qui sont dans le combobox calculer la somme j'essaie de faire de quoi mais ça ne marche pas pouvez vous m'aider voilà mon code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using BLL;
namespace UIL
{
/// <summary>
/// Logique d'interaction pour UCSommeVentesProv.xaml
/// </summary>
///
/// <summary>
/// </summary>
public partial class UCSommeVentesProv : UserControl
{
List<int> listeAnnee = new List<int>();
public UCSommeVentesProv()
{
InitializeComponent();
Vente.ChargerListeAnnee();
Vente.ChargerListeAnneeFin();
Vehicule.ChargerListeVehicule();
//cboAnneeDeb.ItemsSource = Vente.vente;
}
private void cboAnneeDeb_Loaded(object sender, RoutedEventArgs e)
{
cboAnneeDeb.ItemsSource = Vente.ListeanneeDebut;
}
private void cboAnneeDeb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Vente.ChargerListeAnneeFin();
int annee = (cboAnneeDeb.SelectedItem as Vente).AnneeDebut;
for (int i = Vente.ListeanneeFin.Count-1; i >= 0; i--)
{
if (Vente.ListeanneeFin[i].AnneeFin< annee)
{
Vente.ListeanneeFin.RemoveAt(i);
}
}
// cboAnneeFin.ItemsSource = Vente.ListeanneeFin;
/*Vente v = new Vente();
List<int> l = new List<int>();
int annee= (cboAnneeDeb.SelectedItem as Vente).AnneeDebut;
for (int i = 0; i < cboAnneeFin.Items.Count; i++)
{
if (cboAnneeFin.Items[v.AnneeFin] == cboAnneeDeb.SelectedItem)
{
cboAnneeFin.Items.Remove(cboAnneeFin.Items[i]);
i--;
}
}*/
}
private void cboGenre_Loaded(object sender, RoutedEventArgs e)
{
cboGenre.ItemsSource = Vehicule.vehicule;
}
private void cboAnneeFin_Loaded(object sender, RoutedEventArgs e)
{
cboAnneeFin.ItemsSource = Vente.ListeanneeFin;
}
private void btnValider_Click(object sender, RoutedEventArgs e)
{
int valeur1 = (cboAnneeDeb.SelectedItem as Vente).AnneeDebut;
int valeur2 = (cboAnneeFin.SelectedItem as Vente).AnneeFin;
string genre = (cboGenre.SelectedItem as Vehicule).GenreVehicule;
ObservableCollection<int> maliste = new ObservableCollection<int>();
foreach (Vente v in Vente.vente)
{
if ((valeur1< valeur2) && (valeur2 >valeur1))
{
}
}
lsbSomme.ItemsSource = Province.province;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Data;
using MySql.Data.MySqlClient;
using DAL;
namespace BLL
{
/// <summary>
/// </summary>
public class Vente: INotifyPropertyChanged
{
public static ObservableCollection<Vente> vente = new ObservableCollection<Vente>();
public static ObservableCollection<Vente> ListeanneeDebut = new ObservableCollection<Vente>();
public static ObservableCollection<Vente> ListeanneeFin = new ObservableCollection<Vente>();
public static ObservableCollection<Vente> ListeSomme = new ObservableCollection<Vente>();
/* public Vente(string province,string genre,int annee,double nbunites, double montantx1000)
{
this.province = province;
this.genre = genre;
this.annee = annee;
this.nbunites = nbunites;
this.montantx1000 = montantx1000;
}*/
/// <summary>
/// Methode qui est de type "void" qui me permet de récuperer les valeurs de chaque lignes de ma table et ensuite les affecter à mes propriétés à la
/// fin j'ajoute cela à ma liste
/// </summary>
public static void ChargerListeVente()
{
//Création de la collection observable
var listeVente = new ObservableCollection<Vente>();
//Appel de la fonction dans AccessDB pour la connection à la DB
DataTable dt = AccessDB.ConnecterBDVente();
for (int i = 0; i < dt.Rows.Count; i++)
{
var ven = new Vente
{
//Création d'un nouvel objet de type province
LaProvince = new Province(dt.Rows[i]["Province"].ToString()),
//Création d'un nouvel objet de type vehicule
LesGenres = new Vehicule(dt.Rows[i]["Genre"].ToString()),
Annee = Int32.Parse(dt.Rows[i]["Annee"].ToString()),
NbUnites = Int32.Parse(dt.Rows[i]["NbUnites"].ToString()),
Montantx1000 = Int32.Parse(dt.Rows[i]["Montantx1000"].ToString()),
};
listeVente.Add(ven);
}
vente = listeVente;
}
public static void ChargerListeSomme()
{
var listeS = new ObservableCollection<Vente>();
//Appel de la fonction dans AccessDB pour la connection à la DB
DataTable dt = AccessDB.SommeVente();
for (int i = 0; i < dt.Rows.Count; i++)
{
var ven = new Vente
{
SommeMontantx1000 = Int32.Parse(dt.Rows[i]["Montantx1000"].ToString()),
};
listeS.Add(ven);
}
ListeSomme = listeS;
}
public static void ChargerListeAnnee()
{
//Création de la collection observable
var listeA = new ObservableCollection<Vente>();
//Appel de la fonction dans AccessDB pour la connection à la DB
DataTable dt = AccessDB.Annee();
for (int i = 0; i < dt.Rows.Count; i++)
{
var ven = new Vente
{
//Création d'un nouvel objet de type province
AnneeDebut = Int32.Parse(dt.Rows[i]["Annee"].ToString()),
};
listeA.Add(ven);
}
ListeanneeDebut = listeA;
}
public static void ChargerListeAnneeFin()
{
//Création de la collection observable
// var listeF = new ObservableCollection<Vente>();
ListeanneeFin.Clear();
//Appel de la fonction dans AccessDB pour la connection à la DB
DataTable dt = AccessDB.Annee();
for (int i = 0; i < dt.Rows.Count; i++)
{
var ven = new Vente
{
//Création d'un nouvel objet de type province
AnneeFin = Int32.Parse(dt.Rows[i]["Annee"].ToString()),
};
ListeanneeFin.Add(ven);
}
}
/* private string province;
public string Province
{
get
{
return province;
}
set
{
if (this.province != value)
{
this.province = value;
this.NotifyPropertyChanged("Province");
}
}
}*/
/// <summary>
/// je crée un attribut de type Province et je crée une propriété avec , qui est aussi de type Province
/// </summary>
private Province laProvince;
public Province LaProvince
{
get
{
return laProvince;
}
set
{
if (this.laProvince != value)
{
this.laProvince = value;
this.NotifyPropertyChanged("LaProvince");
}
}
}
/// <summary>
/// je crée un attribut de type Vehicule et je crée une propriété avec, qui est aussi de type Vehicule
/// </summary>
private Vehicule lesGenres;
public Vehicule LesGenres
{
get
{
return lesGenres;
}
set
{
if (this.lesGenres != value)
{
this.lesGenres = value;
this.NotifyPropertyChanged("LesGenres");
}
}
}
private int anneeDebut;
public int AnneeDebut
{
get
{
return anneeDebut;
}
set
{
if (this.anneeDebut != value)
{
this.anneeDebut = value;
this.NotifyPropertyChanged("AnneeDebut");
}
}
}
private int anneeFin;
public int AnneeFin
{
get
{
return anneeFin;
}
set
{
if (this.anneeFin != value)
{
this.anneeFin = value;
this.NotifyPropertyChanged("AnneeFin");
}
}
}
/// <summary>
/// je crée un attribut de type string et je crée une propriété avec, qui est aussi de type string
/// </summary>
private string genre;
public string Genre
{
get
{
return genre;
}
set
{
if (this.genre != value)
{
this.genre = value;
this.NotifyPropertyChanged("Genre");
}
}
}
/// <summary>
/// je crée un attribut de type int et je crée une propriété avec, qui est aussi de type int
/// </summary>
private int annee;
public int Annee
{
get
{
return annee;
}
set
{
if (this.annee != value)
{
this.annee = value;
this.NotifyPropertyChanged("Annee");
}
}
}
/// <summary>
/// je crée un attribut de type int et je crée une propriété avec, qui est aussi de type int
/// </summary>
private int nbunites;
public int NbUnites
{
get
{
return nbunites;
}
set
{
if (this.nbunites != value)
{
this.nbunites = value;
this.NotifyPropertyChanged("NbUnites");
}
}
}
/// <summary>
/// je crée un attribut de type int et je crée une propriété avec, qui est aussi de type int
/// </summary>
private int montantx1000;
public int Montantx1000
{
get
{
return montantx1000;
}
set
{
if (this.montantx1000 != value)
{
this.montantx1000 = value;
this.NotifyPropertyChanged("Montantx1000");
}
}
}
public int sommeMontantx1000;
public int SommeMontantx1000
{
get
{
return sommeMontantx1000;
}
set
{
if (this.sommeMontantx1000 != value)
{
this.sommeMontantx1000 = value;
this.NotifyPropertyChanged("SommeMontantx1000");
}
}
}
public void NotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Data;
using MySql.Data.MySqlClient;
using DAL;
namespace BLL
{
//Pour que les objets de cette classe soient capables d'averir l'interface utilisateur des changements de ses propriétés
// cette classe doit avoir l'interface INotifyPropertyChanged
public class Province:INotifyPropertyChanged
{
/// <summary>
/// </summary>
//Création de la collection observable qui est de type province
public static ObservableCollection<Province> province = new ObservableCollection<Province>();
public Province(string nomProvince,string capitale,int superficie,int population)
{
this.NomProvince = nomProvince;
this.Capitale = capitale;
this.Superficie = superficie;
this.Population = population;
}
public Province()
{
}
/// <summary>
/// Constructeur qui initialise juste le nom de la province je vais m'en servir lors du chargement de la liste des ventes
/// </summary>
/// <param name="nomProvince"></param>
public Province(string nomProvince)
{
this.NomProvince = nomProvince;
}
/// <summary>
/// Methode qui est de type "void" qui me permet de récuperer les valeurs de chaque lignes de ma table et ensuite les affecter à mes propriétés à la
/// fin j'ajoute cela à ma liste
/// </summary>
public static void ChargerListeProvince()
{
var listeProvince = new ObservableCollection<Province>();
//Appel de la fonction dans AccessDB pour la connection à la DB
DataTable dt = AccessDB.ConnecterBDProvince();
for (int i = 0; i < dt.Rows.Count; i++)
{
var etu = new Province
{
NomProvince = dt.Rows[i]["nomProvince"].ToString(),
Capitale = dt.Rows[i]["capitale"].ToString(),
Superficie = Int32.Parse(dt.Rows[i]["superficie"].ToString()),
Population = Int32.Parse(dt.Rows[i]["population"].ToString()),
};
listeProvince.Add(etu);
}
province = listeProvince;
}
/// <summary>
/// Cette méthoe me permet de charger une liste des noms de province pour ensuite l'utiliser plustard
/// </summary>
public static void ChargerNomProvince()
{
//Appel de la fonction dans AccessDB pour la connection à la DB
var listeProvince = new ObservableCollection<Province>();
DataTable dt = AccessDB.ConnecterBDProvince();
for (int i = 0; i < dt.Rows.Count; i++)
{
var etu = new Province(dt.Rows[i]["nomProvince"].ToString());
listeProvince.Add(etu);
}
province = listeProvince;
}
/// <summary>
/// je crée un attribut de type string et je crée une propriété avec, qui est aussi de type string
/// </summary>
private string nomProvince;
public string NomProvince
{
get
{
return nomProvince;
}
set
{
if (this.nomProvince != value)
{
this.nomProvince = value;
this.NotifyPropertyChanged("NomProvince");
}
}
}
/// <summary>
/// je crée un attribut de type string et je crée une propriété avec, qui est aussi de type string
/// </summary>
private string capitale;
public string Capitale
{
get
{
return capitale;
}
set
{
if (this.capitale != value)
{
this.capitale = value;
this.NotifyPropertyChanged("Capitale");
}
}
}
/// <summary>
/// je crée un attribut de type int et je crée une propriété avec, qui est aussi de type int
/// </summary>
private int superficie;
public int Superficie
{
get
{
return superficie;
}
set
{
if (this.superficie != value)
{
this.superficie = value;
this.NotifyPropertyChanged("Superficie");
}
}
}
/// <summary>
/// je crée un attribut de type int et je crée une propriété avec, qui est aussi de type int
/// </summary>
private int population;
public int Population
{
get
{
return population;
}
set
{
if (this.population != value)
{
this.population = value;
this.NotifyPropertyChanged("Population");
}
}
}
public void NotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Data;
using System.IO;
using System.Collections.ObjectModel;
using MySql.Data.MySqlClient;
using System.Diagnostics;
namespace DAL
{
public class AccessDB
{
/// <summary>
/// Nom:Daniel Kabeya
/// Matricule:1812147
/// </summary>
/// <summary>
/// Cette méthode me permet de me connecter à la base de données mesventes
/// Vu que j'ai trois tables j'ai créé trois méthodes qui me permettront de me connecter
/// </summary>
public static DataTable ConnecterBDVehicule()
{
MySqlConnection conn = new MySqlConnection("SERVER=localhost;DATABASE=mesventes;UID=root;PASSWORD=;");
try
{
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT genreVehicule FROM vehicule", conn);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet(); // using System.Data
adp.Fill(ds, "vehicule");
var dt = ds.Tables["vehicule"];
return dt;
}
catch (MySqlException ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
conn.Close();
}
return null;
}
/// <summary>
/// Cette méthode me permet de me connecter à la base de données mesventes
/// </summary>
public static DataTable ConnecterBDProvince()
{
MySqlConnection conn = new MySqlConnection("SERVER=localhost;DATABASE=mesventes;UID=root;PASSWORD=;");
try
{
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT nomProvince,capitale,superficie,population FROM province", conn);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet(); // using System.Data
adp.Fill(ds, "province");
var dt = ds.Tables["province"];
return dt;
}
catch (MySqlException ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
conn.Close();
}
return null;
}
/// <summary>
/// Cette méthode aussi me permet de me connecter à la base de données mesventes
/// </summary>
public static DataTable ConnecterBDVente()
{
MySqlConnection conn = new MySqlConnection("SERVER=localhost;DATABASE=mesventes;UID=root;PASSWORD=;");
try
{
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT Province,Genre,Annee,NbUnites,Montantx1000 FROM ventes", conn);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet(); // using System.Data
adp.Fill(ds, "ventes");
var dt = ds.Tables["ventes"];
return dt;
}
catch (MySqlException ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
conn.Close();
}
return null;
}
public static DataTable SommeVente()
{
MySqlConnection conn = new MySqlConnection("SERVER=localhost;DATABASE=mesventes;UID=root;PASSWORD=;");
try
{
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT Montantx1000 SUM (Montantx1000) FROM ventes GROUP BY Montantx1000", conn);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet(); // using System.Data
adp.Fill(ds, "ventes");
var dt = ds.Tables["ventes"];
return dt;
}
catch (MySqlException ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
conn.Close();
}
return null;
}
public static DataTable Annee()
{
MySqlConnection conn = new MySqlConnection("SERVER=localhost;DATABASE=mesventes;UID=root;PASSWORD=;");
try
{
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT Distinct Annee FROM ventes", conn);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet(); // using System.Data
adp.Fill(ds, "ventes");
var dt = ds.Tables["ventes"];
return dt;
}
catch (MySqlException ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
conn.Close();
}
return null;
}
}
}
Whismeril
Messages postés19028Date d'inscriptionmardi 11 mars 2003StatutNon membreDernière intervention24 avril 2024931 19 mai 2021 à 07:45
Bonjour Danny
depuis des semaines je suis le seul (ou quasiment le seul) à te répondre.
Force est de constater que tu poses des questions et ne tient pas compte des réponses
Là par exemple https://forums.commentcamarche.net/forum/affich-37211715-utiliser-deux-combobox je t'ai montré que la liste d'années de début et celle de fin ne doivent pas être chargées depuis la base de données mais calculées à chaque modification de année de début ou année de fin. Au passage, du coup, ça n'a pas besoin d'être une observablecollection
Et dans ce code, ni Linq (qui pourtant répond à ton besoin) ni calcul sur tes listes d'années.
Au total, j'ai pris plusieurs heures sur mon temps personnel pour essayer de t'aider, mais si tu n'en fais qu'à ta tête, ça ne me donne pas envie de continuer.
19 mai 2021 à 07:46
19 mai 2021 à 07:53