Inserer une matiere dans datagridview en C#

Résolu
kfaycal Messages postés 44 Date d'inscription vendredi 16 avril 2021 Statut Membre Dernière intervention 24 mai 2021 - Modifié le 16 avril 2021 à 13:28
vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 - 17 avril 2021 à 12:26
bonjour. comment vous allez? j'aimerais insérer une matière dans mon datagridview mais a chaque fois le datagridview affiche plusieurs alors que dans la base de donnée il y'a que une donnée insérée

6 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 16 avril 2021 à 18:21
Bonjour
A tester sans certitude
public void affichermat()
        {
           dgvmatiere.Rows.Clear(); // on vide le Datagridview 
           dr = dh.ExecuteReader("select code_matiere , nom_matiere , code_unite  , code_filiere , code_niveau  from matiere ");
            while (dr.Read())
            {
                dgvmatiere.Rows.Add(false, dr.GetValue(0).ToString(), dr.GetValue(1).ToString(), dr.GetValue(2).ToString(), dr.GetValue(3).ToString(), dr.GetValue(4).ToString());
            }
            dr.Close();
            lbtot.Text = dgvmatiere.RowCount.ToString();

        }


1
vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 169
16 avril 2021 à 15:40
Bonjour
Dans ton exemple avant il y a 7 enregistrements dans ta base de données .
Après il y en a 15 . On a l'impression que les 7 d'avant sont en double ce qui fait (2 * 7) + 1 = 15 enregistrements .
Sans code difficile de t'en dire plus .
Pour poster du code prière de suivre ce tuto : https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
Merci
0
kfaycal Messages postés 44 Date d'inscription vendredi 16 avril 2021 Statut Membre Dernière intervention 24 mai 2021
16 avril 2021 à 16:31
Offet dans la base de donnée il y a pas de doublons. mais le fait d'afficher les données sur le datagridview , il se double a chaque fois.
0
kfaycal Messages postés 44 Date d'inscription vendredi 16 avril 2021 Statut Membre Dernière intervention 24 mai 2021
Modifié le 16 avril 2021 à 16:50
voici mon code source
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 WindowsFormsApp1.BLL;
using WindowsFormsApp1.DAL;
using WindowsFormsApp1.TOOL;
using MySql.Data.MySqlClient;

/////////////////////////////formulaire pour la liste matiere

namespace WindowsFormsApp1
{
    public partial class UCmatiere : UserControl
    {
        databasehelper dh = new databasehelper();
       
        MySqlDataReader dr;

        public UCmatiere()
        {
            InitializeComponent();
        }
        private static UCmatiere umat;
        //creer une instance pour le usercontrol
        public static UCmatiere instance
        {
            get
            {
                if (umat == null)
                {
                    umat = new UCmatiere();
                }
                return umat;
            }
        }

      

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                ajoutmatiere am = new ajoutmatiere(this);
                am.ShowDialog();
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

      

        }
        public void affichermat()
        {
            dr = dh.ExecuteReader("select code_matiere , nom_matiere , code_unite  , code_filiere , code_niveau  from matiere ");
            while (dr.Read())
            {
                dgvmatiere.Rows.Add(false, dr.GetValue(0).ToString(), dr.GetValue(1).ToString(), dr.GetValue(2).ToString(), dr.GetValue(3).ToString(), dr.GetValue(4).ToString());
            }
            dr.Close();
            lbtot.Text = dgvmatiere.RowCount.ToString();

        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

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

        private void UCmatiere_Load(object sender, EventArgs e)
        {
            affichermat();
        }
    }
}

/////////////////////////////////////////formulaire pour ajouter matiere

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

        private UserControl ajoutmat;
        public ajoutmatiere(UserControl amat)
        {
            InitializeComponent();
            this.ajoutmat = amat;
            txtcodematiere.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {



                if (txtmatiere.Text == "" || txtnomunite.Text == "" || txtnomfiliere.Text == "" || txtnomniveau.Text == "")
                {
                    MessageBox.Show("Désolé tous les champs doivent être renseignés");
                }
                else
                {
                    string codM = "mat" + tool.Max_Cle("code_matiere", 3);
                    dh.ExecuteNonQuery("insert into matiere (code_matiere, nom_matiere, code_unite, code_filiere,code_niveau) values ( '" + codM + "','" + txtmatiere.Text.Replace("'", "''") + "','" + txtnomunite.Text.Replace("'", "''") + "','" + txtnomfiliere.Text.Replace("'", "''") + "','" + txtnomniveau.Text.Replace("'", "''") + "')");

                    MessageBox.Show("Matière enregistré avec succès");
                    (ajoutmat as UCmatiere).affichermat();

                }




            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ajoutmatiere_Load(object sender, EventArgs e)
        {
            try
            {
                //charger les noms des unites d'enseignement 
                ds1 = dh.ExecuteDataSet("select distinct nom_unite from unite order by nom_unite desc");
                txtnomunite.DataSource = ds1.Tables[0];
                txtnomunite.DisplayMember = "nom_unite";
                txtnomunite.Text = "";

                //charger les noms des filieres 
                ds2 = dh.ExecuteDataSet("select distinct abrege from filiere order by abrege desc");
                txtnomfiliere.DataSource = ds2.Tables[0];
                txtnomfiliere.DisplayMember = "abrege";
                txtnomfiliere.Text = "";

                //charger les noms des niveaux
                ds3 = dh.ExecuteDataSet("select distinct abrege from niveau order by abrege asc");
                txtnomniveau.DataSource = ds3.Tables[0];
                txtnomniveau.DisplayMember = "abrege";
                txtnomniveau.Text = "";


            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                txtmatiere.Text = "";
                txtnomfiliere.Text = "";
                txtnomniveau.Text = "";
                txtnomunite.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
kfaycal Messages postés 44 Date d'inscription vendredi 16 avril 2021 Statut Membre Dernière intervention 24 mai 2021
17 avril 2021 à 12:14
Merci infiniment
0
vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 169
17 avril 2021 à 12:26
De rien !
On est là pour cela .
Reste à mettre le sujet "en résolu" en cliquant sur le bouton adéquat


0
Rejoignez-nous