Comment : exécuter une action personnalisée basée sur les modifications apportées dans une cellule d'un contrôle DataGridView Windows Form
Le DataGridView contrôle a un certain nombre d’événements que vous pouvez utiliser pour détecter les modifications dans l’état des DataGridView cellules. Deux des plus couramment utilisés sont les événements et CellStateChanged les CellValueChanged événements.
Pour détecter les modifications dans les valeurs des cellules DataGridView
Écrivez un gestionnaire pour l’événement CellValueChanged .
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { string msg = String.Format( "Cell at row {0}, column {1} value changed", e.RowIndex, e.ColumnIndex); MessageBox.Show(msg, "Cell Value Changed"); }
Private Sub dataGridView1_CellValueChanged(ByVal sender As Object, _ ByVal e As DataGridViewCellEventArgs) _ Handles dataGridView1.CellValueChanged Dim msg As String = String.Format( _ "Cell at row {0}, column {1} value changed", _ e.RowIndex, e.ColumnIndex) MessageBox.Show(msg, "Cell Value Changed") End Sub
Pour détecter les modifications dans les états des cellules DataGridView
Écrivez un gestionnaire pour l’événement CellStateChanged .
private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e) { DataGridViewElementStates state = e.StateChanged; string msg = String.Format("Row {0}, Column {1}, {2}", e.Cell.RowIndex, e.Cell.ColumnIndex, e.StateChanged); MessageBox.Show(msg, "Cell State Changed"); }
Private Sub dataGridView1_CellStateChanged(ByVal sender As Object, _ ByVal e As DataGridViewCellStateChangedEventArgs) _ Handles dataGridView1.CellStateChanged Dim state As DataGridViewElementStates = e.StateChanged Dim msg As String = String.Format( _ "Row {0}, Column {1}, {2}", _ e.Cell.RowIndex, e.Cell.ColumnIndex, e.StateChanged) MessageBox.Show(msg, "Cell State Changed") End Sub
Compilation du code
Cet exemple nécessite :
un contrôle DataGridView nommé
dataGridView1
; Pour C#, les gestionnaires d’événements doivent être connectés aux événements correspondants.des références aux assemblys System et System.Windows.Forms.
Voir aussi
.NET Desktop feedback
Commentaires
https://aka.ms/ContentUserFeedback.
Bientôt disponible : Tout au long de 2024, nous allons supprimer progressivement GitHub Issues comme mécanisme de commentaires pour le contenu et le remplacer par un nouveau système de commentaires. Pour plus d’informations, consultezEnvoyer et afficher des commentaires pour