/// <summary> /// S'effectue lorsque la sélection de la question change. /// Affiche les réponses de la nouvelle question sélectionée. /// </summary> /// <param name="sender">Objet</param> /// <param name="e">Evenement</param> private void dgvQuestion_SelectionChanged(object sender, EventArgs e) { if (dgvQuestion.SelectedRows.Count > 0) { Clear = true; dgvReponse.Rows.Clear(); dgvReponse.AllowUserToAddRows = true; dgvReponse.AllowUserToDeleteRows = true; Clear = false; foreach (KeyValuePair <int, ReponseDatas> item in QcmController.GetReponsesByIdQuestion(Convert.ToInt32(dgvQuestion.SelectedRows[0].Cells[0].Value))) { dgvReponse.Rows.Add(new string[] { item.Key.ToString(), item.Value.Reponse, item.Value.BonneReponse.ToString() }); } if (dgvReponse.Rows.Count - ((dgvReponse.AllowUserToAddRows) ? 1 : 0) /*AddedRow*/ == 4 /*Ligne qui se fait supprimer*/) { dgvReponse.AllowUserToDeleteRows = false; } if (dgvReponse.Rows.Count - ((dgvReponse.AllowUserToAddRows) ? 1 : 0) /*AddedRow*/ == 6 /*Ligne qui se fait supprimer*/) { dgvReponse.AllowUserToAddRows = false; } } }
/// <summary> /// S'effectue après la validation d'une ligne de la DataGridView "dgvReponse". /// Cette méthode est une des plus importantes, car elle permet la création et l'édition du modèle. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgvReponse_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { if (!Clear) { if (dgvReponse.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null) { if (dgvReponse.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Trim() != "") { if (dgvReponse.Rows[e.RowIndex].Cells[1].Value != null) { int idQuestion = Convert.ToInt32(dgvQuestion.SelectedRows[0].Cells[0].Value); string reponseText = dgvReponse.Rows[e.RowIndex].Cells[1].Value.ToString(); bool bonneReponse = Convert.ToBoolean(dgvReponse.Rows[e.RowIndex].Cells[2].Value); if (reponseText.Trim() != "") { if (dgvReponse.Rows[e.RowIndex].Cells[0].Value == null) //Create { string returnText = QcmController.InsertReponse(idQuestion, reponseText, bonneReponse); if (returnText == "") { MessageBox.Show("Réponse créée avec succès !"); dgvReponse.Rows[e.RowIndex].Cells[0].Value = QcmController.GetReponsesByIdQuestion(idQuestion).Last().Key; } else { MessageBox.Show(returnText); e.Cancel = true; } } else { KeyValuePair <bool, string> retour = QcmController.UpdateReponseByIdQuestionAndIdReponse(idQuestion, Convert.ToInt32(dgvReponse.Rows[e.RowIndex].Cells[0].Value), new ReponseDatas(reponseText, bonneReponse, Modes.Update)); if (retour.Value != null) { MessageBox.Show(retour.Value); if (retour.Key) { e.Cancel = true; } } } } } } } } }