DataGridView.CommitEdit(DataGridViewDataErrorContexts) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Valide les modifications apportées à la cellule active dans le cache de données sans mettre fin au mode d’édition.
public:
bool CommitEdit(System::Windows::Forms::DataGridViewDataErrorContexts context);
public bool CommitEdit(System.Windows.Forms.DataGridViewDataErrorContexts context);
member this.CommitEdit : System.Windows.Forms.DataGridViewDataErrorContexts -> bool
Public Function CommitEdit (context As DataGridViewDataErrorContexts) As Boolean
Paramètres
- context
- DataGridViewDataErrorContexts
Combinaison de valeurs au niveau du DataGridViewDataErrorContexts bit qui spécifie le contexte dans lequel une erreur peut se produire.
Retours
true si les modifications ont été validées ; sinon false.
Exceptions
La valeur de cellule n’a pas pu être validée et il n’existe aucun gestionnaire pour l’événement DataError ou le gestionnaire a défini la ThrowException propriété truesur .
Exemples
L’exemple de code suivant appelle la CommitEdit méthode dans un gestionnaire d’événements CurrentCellDirtyStateChanged pour déclencher l’événement CellValueChanged . Cet exemple de code fait partie d’un exemple plus large fourni dans How to : Disable Buttons in a Button Column in the Windows Forms DataGridView Control.
// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
// If a check box cell is clicked, this event handler disables
// or enables the button in the same row as the clicked cell.
public void dataGridView1_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes")
{
DataGridViewDisableButtonCell buttonCell =
(DataGridViewDisableButtonCell)dataGridView1.
Rows[e.RowIndex].Cells["Buttons"];
DataGridViewCheckBoxCell checkCell =
(DataGridViewCheckBoxCell)dataGridView1.
Rows[e.RowIndex].Cells["CheckBoxes"];
buttonCell.Enabled = !(Boolean)checkCell.Value;
dataGridView1.Invalidate();
}
}
' This event handler manually raises the CellValueChanged event
' by calling the CommitEdit method.
Sub dataGridView1_CurrentCellDirtyStateChanged( _
ByVal sender As Object, ByVal e As EventArgs) _
Handles dataGridView1.CurrentCellDirtyStateChanged
If dataGridView1.IsCurrentCellDirty Then
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
' If a check box cell is clicked, this event handler disables
' or enables the button in the same row as the clicked cell.
Public Sub dataGridView1_CellValueChanged(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles dataGridView1.CellValueChanged
If dataGridView1.Columns(e.ColumnIndex).Name = "CheckBoxes" Then
Dim buttonCell As DataGridViewDisableButtonCell = _
CType(dataGridView1.Rows(e.RowIndex).Cells("Buttons"), _
DataGridViewDisableButtonCell)
Dim checkCell As DataGridViewCheckBoxCell = _
CType(dataGridView1.Rows(e.RowIndex).Cells("CheckBoxes"), _
DataGridViewCheckBoxCell)
buttonCell.Enabled = Not CType(checkCell.Value, [Boolean])
dataGridView1.Invalidate()
End If
End Sub
Remarques
Cette méthode tente de convertir la valeur spécifiée par l’utilisateur au type de données de cellule sous-jacent. Pour ce faire, il déclenche l’événement CellParsing , que vous pouvez gérer pour personnaliser la conversion de type. Sinon, les convertisseurs de type par défaut sont utilisés. Les erreurs de conversion peuvent entraîner une exception si l’événement DataError n’est pas géré pour l’empêcher. Si la valeur est correctement convertie, elle est validée dans le magasin de données, ce qui déclenche l’événement CellValuePushed pour les cellules non liées aux données lorsque la valeur de la VirtualMode propriété est true. Si la valeur est validée avec succès, l’événement CellValueChanged se produit.