C# 获取ComboBox值并使用它

C# 获取ComboBox值并使用它,c#,listview,combobox,C#,Listview,Combobox,伙计们,我需要帮助!我的IHM上有几个组合框,它们的数据来自PhpMyAdmin中的每个不同表。我希望当我按下我的搜索按钮时,程序会考虑用户预先输入的组合框的值,以便在数据库中检索它们,从而在我的列表视图中显示它们。 我决定创建一个包含两行的表。第一个包含组合框的名称,第二个包含内容。我不知道这是不是最好的解决办法 表格如下: 以下是我现有的代码: 谢谢你的帮助 using System; using System.Collections.Generic; using System.Compo

伙计们,我需要帮助!我的IHM上有几个组合框,它们的数据来自PhpMyAdmin中的每个不同表。我希望当我按下我的
搜索
按钮时,程序会考虑用户预先输入的组合框的值,以便在数据库中检索它们,从而在我的列表视图中显示它们。 我决定创建一个包含两行的表。第一个包含组合框的名称,第二个包含内容。我不知道这是不是最好的解决办法

表格如下:

以下是我现有的代码:

谢谢你的帮助

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 MySql.Data.MySqlClient;
namespace PROJET_FINDER
{
    public partial class Recherche : DevExpress.XtraEditors.XtraForm
    {
        public Recherche()
        {
            InitializeComponent();
        }

        MySqlConnection connection = new MySqlConnection("Datasource = localhost;port=3306;username=root;password=");

        private void Recherche_Load(object sender, EventArgs e)//Début des appels des ComboBox
        {

            try
            {
                string selectQuery = "SELECT * FROM cdh.etat";                                                     //Début Try/Catch pour le ComboBox le Code d'Etat
                connection.Open();                                                                                      //Connexion à la base de donnée ouverte
                CodeEtatFS_ComboBox_Home.DropDownStyle = ComboBoxStyle.DropDownList;                                    //ReadOnly sur la comboBox
                MySqlCommand command = new MySqlCommand(selectQuery, connection);                                       //Création de l'objet de la commande SQL
                MySqlDataReader reader = command.ExecuteReader();                                                       //Déclaration de la nouvelle instance
                while (reader.Read())
                {
                    CodeEtatFS_ComboBox_Home.Items.Add(reader.GetString("Lib_Code_Etat"));
                }
                connection.Close();
            }
            catch (MySqlException cn)
            {
                MessageBox.Show(cn.Message);
                connection.Close();
            }
            try                                                                                                         //Début Try/Catch pour le ComboBox du numéro FS 
            {
                connection.Open();
                NuméroFS_ComboBox_Home.DropDownStyle = ComboBoxStyle.DropDownList;                                      //ReadOnly sur la comboBox
                string selectQuery = "SELECT * FROM cdh.fiche";
                MySqlCommand command = new MySqlCommand(selectQuery, connection);
                MySqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    NuméroFS_ComboBox_Home.Items.Add(reader.GetString("FS"));
                }
                connection.Close();
            }
            catch (MySqlException cn)
            {
                MessageBox.Show(cn.Message);
                connection.Close();
            }
            try                                                                                                         //Début Try/Catch pour le ComboBox des catégorie Radio
            {
                connection.Open();
                CatégorieRadiologique_ComboBox_Home.DropDownStyle = ComboBoxStyle.DropDownList;                        //ReadOnly sur la comboBox
                string selectQuery = "SELECT * FROM cdh.ctg_rad";
                MySqlCommand command = new MySqlCommand(selectQuery, connection);
                MySqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    CatégorieRadiologique_ComboBox_Home.Items.Add(reader.GetString("Radiologique"));
                }
                connection.Close();
            }
            catch (MySqlException cn)
            {
                MessageBox.Show(cn.Message);
                connection.Close();
            }
            try                                                                                                         //Début Try/Catch pour le ComboBox des Centres
            {
                connection.Open();
                Centre_ComboBox_Home.DropDownStyle = ComboBoxStyle.DropDownList;                                        //ReadOnly sur la comboBox
                string selectQuery = "SELECT * FROM cdh.site_centre";
                MySqlCommand command = new MySqlCommand(selectQuery, connection);
                MySqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Centre_ComboBox_Home.Items.Add(reader.GetString("Centre"));
                }
                connection.Close();
            }
            catch (MySqlException cn)
            {
                MessageBox.Show(cn.Message);
                connection.Close();
            }
            try                                                                                                         //Début Try/Catch pour le ComboBox des producteurs du déchet
            {
                connection.Open();
                Producteur_ComboBox_Home.DropDownStyle = ComboBoxStyle.DropDownList;                                    //ReadOnly sur la comboBox
                string selectQuery = "SELECT * FROM cdh.prod";
                MySqlCommand command = new MySqlCommand(selectQuery, connection);
                MySqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Producteur_ComboBox_Home.Items.Add(reader.GetString("Producteur"));
                }
                connection.Close();
            }
            catch (MySqlException cn)
            {
                MessageBox.Show(cn.Message);
                connection.Close();
            }
            try                                                                                                         //Début Try/Catch pour le ComboBox du conditionnement du déchet
            {
                connection.Open();
                Conditionnement_ComboBox_Home.DropDownStyle = ComboBoxStyle.DropDownList;                               //ReadOnly sur la comboBox
                string selectQuery = "SELECT * FROM cdh.statut_condition";
                MySqlCommand command = new MySqlCommand(selectQuery, connection);
                MySqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Conditionnement_ComboBox_Home.Items.Add(reader.GetString("Conditionnement"));
                }
                connection.Close();
            }
            catch (MySqlException cn)
            {
                MessageBox.Show(cn.Message);
                connection.Close();
            }
            try                                                                                                         //Début Try/Catch pour le ComboBox de la station de destination du déchet
            {
                connection.Open();
                StationDestination_ComboBox_Home.DropDownStyle = ComboBoxStyle.DropDownList;                            //ReadOnly sur la comboBox
                string selectQuery = "SELECT * FROM cdh.station_destination";
                MySqlCommand command = new MySqlCommand(selectQuery, connection);
                MySqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    StationDestination_ComboBox_Home.Items.Add(reader.GetString("Station_Destination"));
                }
                connection.Close();
            }
            catch (MySqlException cn)
            {
                MessageBox.Show(cn.Message);
                connection.Close();
            }
        }//Fin des appels des ComboBox

        private void DataTimePicker_Home_1_ValueChanged(object sender, EventArgs e)                                     //Lorsque la valeur du DataTimePicker_Home_1 change alors le 3 et le 4 se désactive
        {
            DataTimePicker_Home_3.Enabled = false;
            DataTimePicker_Home_4.Enabled = false;
            if(DataTimePicker_Home_3.Enabled == false)
            {

            }
        }

        private void DataTimePicker_Home_2_ValueChanged(object sender, EventArgs e)                                     //Lorsque la valeur du DataTimePicker_Home_2 change alors le 3 et le 4 se désactive
        {
            DataTimePicker_Home_3.Enabled = false;
            DataTimePicker_Home_4.Enabled = false;
        }

        private void DataTimePicker_Home_3_ValueChanged(object sender, EventArgs e)                                     //Lorsque la valeur du DataTimePicker_Home_3 change alors le 1 et le 2 se désactive
        {
            DataTimePicker_Home_1.Enabled = false;
            DataTimePicker_Home_2.Enabled = false;
        }

        private void DataTimePicker_Home_4_ValueChanged(object sender, EventArgs e)                                     //Lorsque la valeur du DataTimePicker_Home_4 change alors le 1 et le 2 se désactive
        {
            DataTimePicker_Home_1.Enabled = false;
            DataTimePicker_Home_2.Enabled = false;
        }  

        private void Data_Reset_Button_Click(object sender, EventArgs e)                                                //Bouton de réinitiallisation des DataTimePicker
        {
            DataTimePicker_Home_1.Enabled = true;
            DataTimePicker_Home_2.Enabled = true;
            DataTimePicker_Home_3.Enabled = true;
            DataTimePicker_Home_4.Enabled = true;
        }

        private void Chercher_Button_Click(object sender, EventArgs e)                                                  //Déclaration du bouton RECHERCHER
        {
            Object selectedItem_Etat = CodeEtatFS_ComboBox_Home.SelectedItem;
            Object selectedItem_Centre = Centre_ComboBox_Home.SelectedItem;
            Object selectedItem_CatRadg = CatégorieRadiologique_ComboBox_Home.SelectedItem;
            Object selectedItem_FS = NuméroFS_ComboBox_Home.SelectedItem;
            Object selectedItem_StationdeDestination = StationDestination_ComboBox_Home.SelectedItem;
            Object selectedItem_Producteur = Producteur_ComboBox_Home.SelectedItem;
            Object selectedItem_Conditionnement = Conditionnement_ComboBox_Home.SelectedItem;

            object[,] Tab_ComboBox =
           {
                {selectedItem_Etat, CodeEtatFS_ComboBox_Home},

                {selectedItem_Centre, Centre_ComboBox_Home},

                {selectedItem_FS, NuméroFS_ComboBox_Home},

                {selectedItem_CatRadg, CatégorieRadiologique_ComboBox_Home},

                {selectedItem_StationdeDestination, StationDestination_ComboBox_Home},

                {selectedItem_Producteur, Producteur_ComboBox_Home},

                {selectedItem_Conditionnement, Conditionnement_ComboBox_Home}
            };



            int i = 0;

            bool statement = false;


            while (i == 7)
            {
                connection.Open();
                if (selectedItem_Etat != null)
                {
                    LW_FS_Home.Items.Clear();
                    MySqlCommand cmd = new MySqlCommand("SELECT * FROM etat WHERE Lib_Code_Etat=@Lib_Code_Etat", connection);
                    cmd.Parameters.AddWithValue("@Lib_Code_Etat", CodeEtatFS_ComboBox_Home.Text);//Récupération de l'age souhaité par l'utilisateur
                    statement = true;
                    i++;
                }
                else if (selectedItem_Centre != null)
                {
                    MySqlCommand cmd = new MySqlCommand("SELECT * FROM cdh.site_centre WHERE Centre=@Centre", connection);
                    cmd.Parameters.AddWithValue("@Lib_Code_Etat", Centre_ComboBox_Home.Text);
                    statement = true;
                    i++;
                }
                else if (selectedItem_FS != null)
                {
                    MySqlCommand cmd = new MySqlCommand("SELECT * FROM cdh.fiche WHERE FS=@NuméroFS_ComboBox_Home", connection);
                    cmd.Parameters.AddWithValue("@Lib_Code_Etat", NuméroFS_ComboBox_Home.Text);
                    statement = true;
                    i++;
                }
                else if (selectedItem_CatRadg != null)
                {
                    MySqlCommand cmd = new MySqlCommand("SELECT * FROM cdh.ctg_rad WHERE Radiologique=@Radiologique", connection);
                    cmd.Parameters.AddWithValue("@Lib_Code_Etat", CatégorieRadiologique_ComboBox_Home.Text);
                    statement = true;
                    i++;
                }
                else if (selectedItem_StationdeDestination != null)
                {
                    MySqlCommand cmd = new MySqlCommand("SELECT * FROM cdh.station_destination WHERE Station_Destination=@Station_Destination", connection);
                    cmd.Parameters.AddWithValue("@Lib_Code_Etat", StationDestination_ComboBox_Home.Text);
                    statement = true;
                    i++;
                }
                else if (selectedItem_Producteur != null)
                {
                    MySqlCommand cmd = new MySqlCommand("SELECT * FROM cdh.prod WHERE Producteur=@Producteur", connection);
                    cmd.Parameters.AddWithValue("@Lib_Code_Etat", Producteur_ComboBox_Home.Text);
                    statement = true;
                    i++;
                }
                else if (selectedItem_Conditionnement != null)
                {
                    MySqlCommand cmd = new MySqlCommand("SELECT * FROM cdh.statut_condition WHERE Conditionnement=@Conditionnement", connection);
                    cmd.Parameters.AddWithValue("@Lib_Code_Etat", Conditionnement_ComboBox_Home.Text); 
                    statement = true;
                    i++;
                }
                else
                {
                    MessageBox.Show("AIE");
                }

                foreach (Object Tab_Combo in Tab_ComboBox)
                {
                    MySqlCommand cmd = new MySqlCommand();
                    if (statement == true)
                    {
                        using (MySqlDataReader Lire = cmd.ExecuteReader())
                        {
                            while (Lire.Read())  // Boucle While qui regarde toutes les infos que l'on souhaite voir avec la méthode "lire"
                            {
                                string TAB = Lire["Tab_Combo"].ToString();

                                LW_FS_Home.Items.Add(new ListViewItem(new[] { TAB }));
                            }
                        }
                    }
                }

            }
        }
}