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)
{
}
}
}