Faire un filtre sur un datagridview

Résolu
kfaycal Messages postés 44 Date d'inscription vendredi 16 avril 2021 Statut Membre Dernière intervention 24 mai 2021 - 22 avril 2021 à 17:56
kfaycal Messages postés 44 Date d'inscription vendredi 16 avril 2021 Statut Membre Dernière intervention 24 mai 2021 - 14 mai 2021 à 10:12
Bonjour, comment vous allez?
j'ai essaye de créer une barre de recherche pour pouvoir filtrer les données de la datagridview mais je n'arrive pas.

voici la photo:


voici mon code source:
 
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApp1.BLL;
using WindowsFormsApp1.DAL;
using WindowsFormsApp1.TOOL;
using MySql.Data.MySqlClient;
using WindowsFormsApp1.Windows_Form.utilisateur;

namespace WindowsFormsApp1
{
    public partial class UCutilisateur : UserControl
    {
        //Appel des controleurs , info et chaines de connexion
        databasehelper dh = new databasehelper();
        MySqlDataReader dr;
        operateurcontroleur opc = new operateurcontroleur();
        operateurinfo opi = new operateurinfo();
        FormCollection listform;
        DataSet ds1, ds2, ds3;

        //variable
        bool result;

        public UCutilisateur()
        {
            InitializeComponent();

            //desactive textbox recherche
            txtrecherche.Enabled = false;


        }


        private static UCutilisateur userc;

        //creer une instance pour le usercontrol
        public static UCutilisateur instance
        {
            get 
            {
                if (userc == null)
                { 
                    userc = new UCutilisateur(); 
                }
                return userc;
            }
        }
        

        //verifier combien de ligne est selectionnee
        public string selectmodif()
        {
            int nombreligneselect = 0;
            for (int i = 0; i < dgvuser.Rows.Count; i++)
            {
                if ((bool)dgvuser.Rows[i].Cells[0].Value == true) //si ligne est selectionne
                {
                    nombreligneselect++; //nombreligne+1
                }
            }
            if (nombreligneselect == 0)
            {
                return "Selectionner l'utilisateur que vous voulez modifier";
            }
            if (nombreligneselect > 1)
            {
                return "Selectionner seulement un seul utilisateur pour modifier";
            }

            return null;
        }
        public string selectsupp()
        {
            int nombreligneselect = 0;
            for (int i = 0; i < dgvuser.Rows.Count; i++)
            {
                if ((bool)dgvuser.Rows[i].Cells[0].Value == true) //si ligne est selectionne
                {
                    nombreligneselect++; //nombreligne+1
                }
            }
            if (nombreligneselect == 0)
            {
                return "Selectionner l'utilisateur que vous voulez supprimer";
            }
            if (nombreligneselect > 1)
            {
                return "Selectionner seulement un seul utilisateur pour supprimer";
            }

            return null;
        }
        public string selectajout()
        {
            int nombreligneselect = 0;
            for (int i = 0; i < dgvuser.Rows.Count; i++)
            {
                if ((bool)dgvuser.Rows[i].Cells[0].Value == true) //si ligne est selectionne
                {
                    nombreligneselect++; //nombreligne+1

                }

            }
            if (nombreligneselect == 0)
            {
                return "Selectionner l'unité d'enseignement que vous voulez supprimer";
            }
            if (nombreligneselect > 1)
            {
                return "Selectionner seulement un seul unité d'enseignement pour supprimer";
            }

            return null;
        }
        private void lstv_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }
        //ajouter dans datagridview
       

        public void affiu()
        {
            dgvuser.Rows.Clear(); //tjours vider le datagridview

            dr = dh.ExecuteReader("select code_user as \"Code User\", nomprenom as \"Nom Prenom\", login as Login, motpasse as Password, profil as Profil, statut as Statut, nom_filiere as Filiere,nom_niveau as Niveau from utilisateur  ");
            while (dr.Read())
            {
                dgvuser.Rows.Add(false, dr.GetValue(0).ToString(), dr.GetValue(1).ToString(), dr.GetValue(2).ToString(), dr.GetValue(3).ToString(), dr.GetValue(4).ToString(), dr.GetValue(5).ToString(), dr.GetValue(6).ToString(), dr.GetValue(7).ToString());
            }
            dr.Close();
            lbtot.Text = dgvuser.RowCount.ToString();
        }
        private void UCutilisateur_Load(object sender, EventArgs e)
        {
            try
            {
                affiu();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

           
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ajoututilisateur au = new ajoututilisateur(this);
                if (selectajout() != null)
                {
                    au.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Veuillez décocher d'abord avant d'ajouter un utilisateur", "Ajout", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
           
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            

        }

        public int idselect;
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                 modifierutilisateur mu = new modifierutilisateur(this);
                if (selectmodif() == null)
                {
                    for (int i = 0; i < dgvuser.Rows.Count; i++)
                    {
                        if ((bool)dgvuser.Rows[i].Cells[0].Value == true) //si le checkbox est vrai, afficher les informations dans le formulaire modifierutilisateur
                        {
                            //idselect = (int)dgvuser.Rows[i].Cells[1].Value;
                            mu.txtcode.Text = dgvuser.Rows[i].Cells[1].Value.ToString();
                            mu.txtnomprenomm.Text = dgvuser.Rows[i].Cells[2].Value.ToString();
                            mu.txtloginm.Text = dgvuser.Rows[i].Cells[3].Value.ToString();
                            mu.txtmotdepassem.Text = dgvuser.Rows[i].Cells[4].Value.ToString();
                            mu.txtprofil.Text = dgvuser.Rows[i].Cells[5].Value.ToString();
                            mu.txtstatutm.Text = dgvuser.Rows[i].Cells[6].Value.ToString();
                            mu.txtfiliere.Text = dgvuser.Rows[i].Cells[7].Value.ToString();
                            mu.txtniveau.Text = dgvuser.Rows[i].Cells[8].Value.ToString();
                        }
                    }

                    mu.ShowDialog();
                

                }
                else
                {
                    MessageBox.Show(selectmodif(), "Modification", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
              

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
       

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                supprimerutilisateur sup = new supprimerutilisateur(this);
                if (selectsupp() == null)
                {
                    for (int i = 0; i < dgvuser.Rows.Count; i++)
                    {
                        if ((bool)dgvuser.Rows[i].Cells[0].Value == true) //si le checkbox est vrai, afficher les informations dans le formulaire modifierutilisateur
                        {
                            //idselect = (int)dgvuser.Rows[i].Cells[1].Value;
                            sup.txtcode.Text = dgvuser.Rows[i].Cells[1].Value.ToString();
                            sup.txtnomprenom.Text = dgvuser.Rows[i].Cells[2].Value.ToString();
                            sup.txtlogin.Text = dgvuser.Rows[i].Cells[3].Value.ToString();
                            sup.txtmotdepasse.Text = dgvuser.Rows[i].Cells[4].Value.ToString();
                            sup.txtprofil.Text = dgvuser.Rows[i].Cells[5].Value.ToString();
                            sup.txtstatut.Text = dgvuser.Rows[i].Cells[6].Value.ToString();
                            sup.txtfiliere.Text = dgvuser.Rows[i].Cells[7].Value.ToString();
                            sup.txtniveau.Text = dgvuser.Rows[i].Cells[8].Value.ToString();
                        }
                    }

                    sup.ShowDialog();


                }
                else
                {
                    MessageBox.Show(selectsupp(), "Suppression", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        
        private void button4_Click(object sender, EventArgs e)
        {
           



        }

        private void dgv(object sender, EventArgs e)
        {
           
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (txtrecherche.Text == "Recherche")
            {
                txtrecherche.Text = "";
                txtrecherche.ForeColor = Color.Black;
            }
           
           
        }

        private void dgvuser_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
///////////////////////////////ma partie pour filtrer les donnees
        private void txtrecherche_TextChanged(object sender, EventArgs e)
        {


           
                if (comborecherche.Text == "Nom et Prenom")
                {
                    userbindingSource1.Filter = "Nom et Prenom like '%" + comborecherche.Text + "&'";
                }
            else if (comborecherche.Text == "Login")
            {
                userbindingSource1.Filter = "Login like '%" + comborecherche.Text + "&'";
            }
            else if (comborecherche.Text == "Profil")
            {
                userbindingSource1.Filter = "Profil like '%" + comborecherche.Text + "&'";
            }
            else if (comborecherche.Text == "Statut")
            {
                userbindingSource1.Filter = "Statut like '%" + comborecherche.Text + "&'";
            }







        }

        private void comborecherche_SelectedIndexChanged(object sender, EventArgs e)
        {
            //activer le textbox recherche si jai choisi un champ
            txtrecherche.Enabled = true;
            txtrecherche.Text = ""; //vider le textbox recherche
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
           
        }

        private void button16_Click(object sender, EventArgs e)
        {
            this.Hide(); 
        }

        private void lstv_selected(object sender, EventArgs e)
        {
           
        }
    }
}




MERCI D'AVANCE

4 réponses

vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 169
Modifié le 22 avril 2021 à 18:25
Bonjour
Veuillez d'abord clôturer les sujets qui peuvent l'être comme je le vous ai déjà précisé dans des messages précédents ( en particulier celui-ci : https://codes-sources.commentcamarche.net/forum/affich-10105172-inserer-une-matiere-dans-datagridview-en-c ) .
Merci

0
Whismeril Messages postés 19022 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 17 avril 2024 656
23 avril 2021 à 08:53
Bonjour

kfaycal, lors de ta précédente question, je t'ai proposé une solution à base de binding et tu as répondu que c'est pas possible parce que tu utilises wampserver (de mon point de vue l'un n'empêche pas l'autre).

Aujourd'hui, tu reposes une question où le binding et une ligne de code répondent à ton besoin....

Et comme cette fois tu accompagnes ta question d'un code, on peut jauger pourquoi le binding n'est pas possible pour toi => tu ne sais pas coder correctement en C#.
Le code que tu montres combine les méthodes d'affichage qu'on utilisait au début des années 1990 avec VB1 (utiliser les contrôles comme des données et les assigner un par un). De plus il ne fait pas appel à des objets métiers et ça c'est le fondement de C#.


Partout dans ce code, le binding réduirait et surtout simplifierait le code.

Tu peux vérifier les 50 ou 100 dernières questions du forum C#, Xavier et NHenry passent de temps à autres, KX parfois, mais les 2 seuls qui sont là presque tous les jours, c'est VB95 et moi.

En ce qui me concerne, je me ferai un plaisir de te guider pour que tu utilises C# comme il faut, à condition bien sûr que tu le veuilles et que tu accepte que ça prendra du temps.

Par contre, il est certain que je ne vais pas m'embêter à me replonger dans des méthodes archaïques pour t'aider.

Donc si tu es OK pour continuer sur la voie que je te propose, va relire https://codes-sources.commentcamarche.net/faq/1291-utilisation-du-binding-au-travers-de-l-objet-databindingsource

Et dis moi quelles sont les similitudes avec ton cas
0
kfaycal Messages postés 44 Date d'inscription vendredi 16 avril 2021 Statut Membre Dernière intervention 24 mai 2021
23 avril 2021 à 11:56
D'accord je vais ouvrir le lien que tu viens de m'envoyer.
Merci
0
kfaycal Messages postés 44 Date d'inscription vendredi 16 avril 2021 Statut Membre Dernière intervention 24 mai 2021
14 mai 2021 à 10:12
Bonjour , je viens de trouver la solution a mon problème.
voici la reponse:
 
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApp1.BLL;
using WindowsFormsApp1.DAL;
using WindowsFormsApp1.TOOL;
using MySql.Data.MySqlClient;
using WindowsFormsApp1.Windows_Form.utilisateur;

namespace WindowsFormsApp1
{
    public partial class UCutilisateur : UserControl
    {
        //Appel des controleurs , info et chaines de connexion
        databasehelper dh = new databasehelper();
        MySqlDataReader dr;
        operateurcontroleur opc = new operateurcontroleur();
        operateurinfo opi = new operateurinfo();
        FormCollection listform;
        DataSet ds1, ds2, ds3;

        //variable
        bool result;

        public UCutilisateur()
        {
            InitializeComponent();

            //desactive textbox recherche
            txtrecherche.Enabled = false;


        }


        private static UCutilisateur userc;

        //creer une instance pour le usercontrol
        public static UCutilisateur instance
        {
            get 
            {
                if (userc == null)
                { 
                    userc = new UCutilisateur(); 
                }
                return userc;
            }
        }
        

        //verifier combien de ligne est selectionnee
        public string selectmodif()
        {
            int nombreligneselect = 0;
            for (int i = 0; i < dgvuser.Rows.Count; i++)
            {
                if ((bool)dgvuser.Rows[i].Cells[0].Value == true) //si ligne est selectionne
                {
                    nombreligneselect++; //nombreligne+1
                }
            }
            if (nombreligneselect == 0)
            {
                return "Selectionner l'utilisateur que vous voulez modifier";
            }
            if (nombreligneselect > 1)
            {
                return "Selectionner seulement un seul utilisateur pour modifier";
            }

            return null;
        }
        public string selectsupp()
        {
            int nombreligneselect = 0;
            for (int i = 0; i < dgvuser.Rows.Count; i++)
            {
                if ((bool)dgvuser.Rows[i].Cells[0].Value == true) //si ligne est selectionne
                {
                    nombreligneselect++; //nombreligne+1
                }
            }
            if (nombreligneselect == 0)
            {
                return "Selectionner l'utilisateur que vous voulez supprimer";
            }
            if (nombreligneselect > 1)
            {
                return "Selectionner seulement un seul utilisateur pour supprimer";
            }

            return null;
        }
        public string selectajout()
        {
            int nombreligneselect = 0;
            for (int i = 0; i < dgvuser.Rows.Count; i++)
            {
                if ((bool)dgvuser.Rows[i].Cells[0].Value == true) //si ligne est selectionne
                {
                    nombreligneselect++; //nombreligne+1

                }

            }
            if (nombreligneselect == 0)
            {
                return "Selectionner l'unité d'enseignement que vous voulez supprimer";
            }
            if (nombreligneselect > 1)
            {
                return "Selectionner seulement un seul unité d'enseignement pour supprimer";
            }

            return null;
        }
        private void lstv_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }
        //ajouter dans datagridview
       

        public void affiu()
        {
            dgvuser.Rows.Clear(); //tjours vider le datagridview

            dr = dh.ExecuteReader("select code_user as \"Code User\", nomprenom as \"Nom Prenom\", login as Login, motpasse as Password, profil as Profil, statut as Statut, nom_filiere as Filiere,nom_niveau as Niveau from utilisateur  ");
            while (dr.Read())
            {
                dgvuser.Rows.Add(false, dr.GetValue(0).ToString(), dr.GetValue(1).ToString(), dr.GetValue(2).ToString(), dr.GetValue(3).ToString(), dr.GetValue(4).ToString(), dr.GetValue(5).ToString(), dr.GetValue(6).ToString(), dr.GetValue(7).ToString());
            }
            dr.Close();
            lbtot.Text = dgvuser.RowCount.ToString();
        }
        private void UCutilisateur_Load(object sender, EventArgs e)
        {
            try
            {
                affiu();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

           
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ajoututilisateur au = new ajoututilisateur(this);
                if (selectajout() != null)
                {
                    au.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Veuillez décocher d'abord avant d'ajouter un utilisateur", "Ajout", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
           
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            

        }

        public int idselect;
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                 modifierutilisateur mu = new modifierutilisateur(this);
                if (selectmodif() == null)
                {
                    for (int i = 0; i < dgvuser.Rows.Count; i++)
                    {
                        if ((bool)dgvuser.Rows[i].Cells[0].Value == true) //si le checkbox est vrai, afficher les informations dans le formulaire modifierutilisateur
                        {
                            //idselect = (int)dgvuser.Rows[i].Cells[1].Value;
                            mu.txtcode.Text = dgvuser.Rows[i].Cells[1].Value.ToString();
                            mu.txtnomprenomm.Text = dgvuser.Rows[i].Cells[2].Value.ToString();
                            mu.txtloginm.Text = dgvuser.Rows[i].Cells[3].Value.ToString();
                            mu.txtmotdepassem.Text = dgvuser.Rows[i].Cells[4].Value.ToString();
                            mu.txtprofil.Text = dgvuser.Rows[i].Cells[5].Value.ToString();
                            mu.txtstatutm.Text = dgvuser.Rows[i].Cells[6].Value.ToString();
                            mu.txtfiliere.Text = dgvuser.Rows[i].Cells[7].Value.ToString();
                            mu.txtniveau.Text = dgvuser.Rows[i].Cells[8].Value.ToString();
                        }
                    }

                    mu.ShowDialog();
                

                }
                else
                {
                    MessageBox.Show(selectmodif(), "Modification", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
              

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
       

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                supprimerutilisateur sup = new supprimerutilisateur(this);
                if (selectsupp() == null)
                {
                    for (int i = 0; i < dgvuser.Rows.Count; i++)
                    {
                        if ((bool)dgvuser.Rows[i].Cells[0].Value == true) //si le checkbox est vrai, afficher les informations dans le formulaire modifierutilisateur
                        {
                            //idselect = (int)dgvuser.Rows[i].Cells[1].Value;
                            sup.txtcode.Text = dgvuser.Rows[i].Cells[1].Value.ToString();
                            sup.txtnomprenom.Text = dgvuser.Rows[i].Cells[2].Value.ToString();
                            sup.txtlogin.Text = dgvuser.Rows[i].Cells[3].Value.ToString();
                            sup.txtmotdepasse.Text = dgvuser.Rows[i].Cells[4].Value.ToString();
                            sup.txtprofil.Text = dgvuser.Rows[i].Cells[5].Value.ToString();
                            sup.txtstatut.Text = dgvuser.Rows[i].Cells[6].Value.ToString();
                            sup.txtfiliere.Text = dgvuser.Rows[i].Cells[7].Value.ToString();
                            sup.txtniveau.Text = dgvuser.Rows[i].Cells[8].Value.ToString();
                        }
                    }

                    sup.ShowDialog();


                }
                else
                {
                    MessageBox.Show(selectsupp(), "Suppression", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        
        private void button4_Click(object sender, EventArgs e)
        {
           



        }

        private void dgv(object sender, EventArgs e)
        {
           
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (txtrecherche.Text == "Recherche")
            {
                txtrecherche.Text = "";
                txtrecherche.ForeColor = Color.Black;
            }
           
           
        }

        private void dgvuser_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
///////////////////////////////ma partie pour filtrer les donnees/////////////////
        private void txtrecherche_TextChanged(object sender, EventArgs e)
        {

  if (comborecherche.Text == "Nom et Prenom")
                {
                dgvuser.Rows.Clear(); //tjours vider le datagridview

                dr = dh.ExecuteReader("select code_user as \"Code User\", nomprenom as \"Nom Prenom\", login as Login, motpasse as Password, profil as Profil, statut as Statut, nom_filiere as Filiere,nom_niveau as Niveau ,profession from utilisateur where nomprenom like '%" + txtrecherche.Text + "%' ");
                while (dr.Read())
                {
                    dgvuser.Rows.Add(false, dr.GetValue(0).ToString(), dr.GetValue(1).ToString(), dr.GetValue(2).ToString(), dr.GetValue(3).ToString(), dr.GetValue(4).ToString(), dr.GetValue(5).ToString(), dr.GetValue(6).ToString(), dr.GetValue(7).ToString());
                }
                dr.Close();
                lbtot.Text = dgvuser.RowCount.ToString();
                
            }
            else if (comborecherche.Text == "Login")
            {
                dgvuser.Rows.Clear(); //tjours vider le datagridview

                dr = dh.ExecuteReader("select code_user as \"Code User\", nomprenom as \"Nom Prenom\", login as Login, motpasse as Password, profil as Profil, statut as Statut, nom_filiere as Filiere,nom_niveau as Niveau,profession  from utilisateur where login like '%" + txtrecherche.Text + "%' ");
                while (dr.Read())
                {
                    dgvuser.Rows.Add(false, dr.GetValue(0).ToString(), dr.GetValue(1).ToString(), dr.GetValue(2).ToString(), dr.GetValue(3).ToString(), dr.GetValue(4).ToString(), dr.GetValue(5).ToString(), dr.GetValue(6).ToString(), dr.GetValue(7).ToString());
                }
                dr.Close();
                lbtot.Text = dgvuser.RowCount.ToString();

            }
            else if (comborecherche.Text == "Profil")
            {
                dgvuser.Rows.Clear(); //tjours vider le datagridview

                dr = dh.ExecuteReader("select code_user as \"Code User\", nomprenom as \"Nom Prenom\", login as Login, motpasse as Password, profil as Profil, statut as Statut, nom_filiere as Filiere,nom_niveau as Niveau,profession  from utilisateur where profil like '%" + txtrecherche.Text + "%' ");
                while (dr.Read())
                {
                    dgvuser.Rows.Add(false, dr.GetValue(0).ToString(), dr.GetValue(1).ToString(), dr.GetValue(2).ToString(), dr.GetValue(3).ToString(), dr.GetValue(4).ToString(), dr.GetValue(5).ToString(), dr.GetValue(6).ToString(), dr.GetValue(7).ToString());
                }
                dr.Close();
                lbtot.Text = dgvuser.RowCount.ToString();

            }
            else if (comborecherche.Text == "Statut")
            {
                dgvuser.Rows.Clear(); //tjours vider le datagridview

                dr = dh.ExecuteReader("select code_user as \"Code User\", nomprenom as \"Nom Prenom\", login as Login, motpasse as Password, profil as Profil, statut as Statut, nom_filiere as Filiere,nom_niveau as Niveau,profession  from utilisateur where statut like '%" + txtrecherche.Text + "%' ");
                while (dr.Read())
                {
                    dgvuser.Rows.Add(false, dr.GetValue(0).ToString(), dr.GetValue(1).ToString(), dr.GetValue(2).ToString(), dr.GetValue(3).ToString(), dr.GetValue(4).ToString(), dr.GetValue(5).ToString(), dr.GetValue(6).ToString(), dr.GetValue(7).ToString());
                }
                dr.Close();
                lbtot.Text = dgvuser.RowCount.ToString();

            }
            else if (comborecherche.Text == "Filière")
            {
                dgvuser.Rows.Clear(); //tjours vider le datagridview

                dr = dh.ExecuteReader("select code_user as \"Code User\", nomprenom as \"Nom Prenom\", login as Login, motpasse as Password, profil as Profil, statut as Statut, nom_filiere as Filiere,nom_niveau as Niveau,profession  from utilisateur where nom_filiere like '%" + txtrecherche.Text + "%' ");
                while (dr.Read())
                {
                    dgvuser.Rows.Add(false, dr.GetValue(0).ToString(), dr.GetValue(1).ToString(), dr.GetValue(2).ToString(), dr.GetValue(3).ToString(), dr.GetValue(4).ToString(), dr.GetValue(5).ToString(), dr.GetValue(6).ToString(), dr.GetValue(7).ToString());
                }
                dr.Close();
                lbtot.Text = dgvuser.RowCount.ToString();

            }
            else if (comborecherche.Text == "Niveau")
            {
                dgvuser.Rows.Clear(); //tjours vider le datagridview

                dr = dh.ExecuteReader("select code_user as \"Code User\", nomprenom as \"Nom Prenom\", login as Login, motpasse as Password, profil as Profil, statut as Statut, nom_filiere as Filiere,nom_niveau as Niveau,profession  from utilisateur where nom_niveau like '%" + txtrecherche.Text + "%' ");
                while (dr.Read())
                {
                    dgvuser.Rows.Add(false, dr.GetValue(0).ToString(), dr.GetValue(1).ToString(), dr.GetValue(2).ToString(), dr.GetValue(3).ToString(), dr.GetValue(4).ToString(), dr.GetValue(5).ToString(), dr.GetValue(6).ToString(), dr.GetValue(7).ToString());
                }
                dr.Close();
                lbtot.Text = dgvuser.RowCount.ToString();

            }
            else if (comborecherche.Text == "Profession")
            {
                dgvuser.Rows.Clear(); //tjours vider le datagridview

                dr = dh.ExecuteReader("select code_user as \"Code User\", nomprenom as \"Nom Prenom\", login as Login, motpasse as Password, profil as Profil, statut as Statut, nom_filiere as Filiere,nom_niveau as Niveau,profession  from utilisateur where profession like '%" + txtrecherche.Text + "%' ");
                while (dr.Read())
                {
                    dgvuser.Rows.Add(false, dr.GetValue(0).ToString(), dr.GetValue(1).ToString(), dr.GetValue(2).ToString(), dr.GetValue(3).ToString(), dr.GetValue(4).ToString(), dr.GetValue(5).ToString(), dr.GetValue(6).ToString(), dr.GetValue(7).ToString());
                }
                dr.Close();
                lbtot.Text = dgvuser.RowCount.ToString();

            }
        }

        private void comborecherche_SelectedIndexChanged(object sender, EventArgs e)
        {
            //activer le textbox recherche si jai choisi un champ
            txtrecherche.Enabled = true;
            txtrecherche.Text = ""; //vider le textbox recherche
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
           
        }

        private void button16_Click(object sender, EventArgs e)
        {
            this.Hide(); 
        }

        private void lstv_selected(object sender, EventArgs e)
        {
           
        }
    }
}
0
Rejoignez-nous