DataGridView.CommitEdit(DataGridViewDataErrorContexts) Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Potvrdí změny v aktuální buňce do mezipaměti dat bez ukončení režimu úprav.
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
Parametry
- context
- DataGridViewDataErrorContexts
Bitová DataGridViewDataErrorContexts kombinace hodnot, která určuje kontext, ve kterém může dojít k chybě.
Návraty
true pokud byly změny potvrzeny; jinak false.
Výjimky
Hodnotu buňky nelze potvrdit a buď neexistuje žádná obslužná rutina události DataError , nebo obslužná rutina nastavila ThrowException vlastnost na true.
Příklady
Následující příklad kódu volá metodu CommitEditCurrentCellDirtyStateChanged v rámci obslužné rutiny události vyvolat CellValueChanged událost. Tento příklad kódu je součástí většího příkladu uvedeného v části Postupy: Zakázání tlačítek ve sloupci tlačítka v ovládacím prvku Windows Forms DataGridView.
// 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
Poznámky
Tato metoda se pokusí převést formátovanou hodnotu zadanou uživatelem na datový typ podkladové buňky. Za tímto účelem vyvolá CellParsing událost, kterou můžete zpracovat pro přizpůsobení převodu typu. V opačném případě se použijí výchozí převaděče typů. Chyby převodu DataError můžou vést k výjimce, pokud se událost nezpracuje, aby se zabránilo. Pokud je hodnota úspěšně převedena, je potvrzena do úložiště dat a vyvolá CellValuePushed událost pro buňky, které nejsou vázané na data, pokud VirtualMode je truehodnota vlastnosti . Pokud je hodnota úspěšně potvrzena, dojde k CellValueChanged události.