string Query = @" SELECT EMPLOYE.numE, EMPLOYE.nom, EMPLOYE.prenom, EMPLOYE.salaire FROM EMPLOYE INNER JOIN SERVICES ON EMPLOYE.NumS = SERVICES.NumS WHERE (SERVICES.NomS = @NomService)"; SqlDataReader reader = null; SqlCommand command = new SqlCommand(Query, connection); command.Parameters.AddWithValue("@NomService", comboService.SelectedItem.ToString()); try { dgv.Rows.Clear(); reader = command.ExecuteReader(); while (reader.Read()) { dgv.Rows.Add(reader[0], reader[1], reader[2], reader[3]); } //Si AllowUserToAddRows alors il y a une ligne de plus que de données. int nb = dgv.AllowUserToAddRows ? dgv.Rows.Count - 1 : dgv.Rows.Count; label2.Text = string.Format("Nombre des enployes pour le service {0} est : {1} ", comboService.Text ,nb); } catch (Exception ex) { MessageBox.Show("Erreur:__ " + ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (reader != null) reader.Close(); }
string Query = @" SELECT EMPLOYE.numE, EMPLOYE.nom, EMPLOYE.prenom, EMPLOYE.salaire FROM EMPLOYE INNER JOIN SERVICES ON EMPLOYE.NumS = SERVICES.NumS WHERE (SERVICES.NomS = @NomService)"; SqlDataReader reader = null; SqlCommand command = new SqlCommand(Query, cnx); command.Parameters.AddWithValue("@NomService", comboService.SelectedItem.ToString()); try { if(cnx.State==0) cnx.Open(); dgv.Rows.Clear(); reader = command.ExecuteReader(); while (reader.Read()) { dgv.Rows.Add(reader[0], reader[1], reader[2], reader[3]); } //Si AllowUserToAddRows alors il y a une ligne de plus que de données. int nb = dgv.AllowUserToAddRows ? dgv.Rows.Count - 1 : dgv.Rows.Count; label2.Text = string.Format("Nombre des enployes pour le service {0} est : {1} ", comboService.Text ,nb); } catch (Exception ex) { MessageBox.Show("Erreur:__ " + ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (reader != null) reader.Close(); cnx.Close(); }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionpublic Form1() { InitializeComponent(); //Création de 4 colonnes DataGridViewTextBoxColumn Column1 = new DataGridViewTextBoxColumn(); Column1.HeaderText = "Numéro"; Column1.Name = "EMPLOYE_numE"; DataGridViewTextBoxColumn Column2 = new DataGridViewTextBoxColumn(); Column2.HeaderText = "Nom"; Column2.Name = "EMPLOYE_nom"; DataGridViewTextBoxColumn Column3 = new DataGridViewTextBoxColumn(); Column3.HeaderText = "Prénom"; Column3.Name = "EMPLOYE_prenom"; DataGridViewTextBoxColumn Column4 = new DataGridViewTextBoxColumn(); Column4.HeaderText = "Salaire"; Column4.Name = "EMPLOYE_salaire"; //Ajout des colonnes dans la DataGridView dgv.Columns.AddRange(new DataGridViewColumn[] { Column1, Column2, Column3, Column4 });