DataGridView.CellValueChanged Událost
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í.
Nastane, když se změní hodnota buňky.
public:
event System::Windows::Forms::DataGridViewCellEventHandler ^ CellValueChanged;
public event System.Windows.Forms.DataGridViewCellEventHandler CellValueChanged;
public event System.Windows.Forms.DataGridViewCellEventHandler? CellValueChanged;
member this.CellValueChanged : System.Windows.Forms.DataGridViewCellEventHandler
Public Custom Event CellValueChanged As DataGridViewCellEventHandler
Event Type
Příklady
Následující příklad kódu ukazuje, jak použít CellValueChanged událost k aktualizaci hodnot ve sloupci zůstatku v objektu DataGridView. Tento příklad je součástí většího příkladu SelectionChanged , který je k dispozici v události.
private void DataGridView1_CellValueChanged(
object sender, DataGridViewCellEventArgs e)
{
// Update the balance column whenever the value of any cell changes.
UpdateBalance();
}
private void DataGridView1_RowsRemoved(
object sender, DataGridViewRowsRemovedEventArgs e)
{
// Update the balance column whenever rows are deleted.
UpdateBalance();
}
private void UpdateBalance()
{
int counter;
int balance;
int deposit;
int withdrawal;
// Iterate through the rows, skipping the Starting Balance row.
for (counter = 1; counter < (DataGridView1.Rows.Count - 1);
counter++)
{
deposit = 0;
withdrawal = 0;
balance = int.Parse(DataGridView1.Rows[counter - 1]
.Cells["Balance"].Value.ToString());
if (DataGridView1.Rows[counter].Cells["Deposits"].Value != null)
{
// Verify that the cell value is not an empty string.
if (DataGridView1.Rows[counter]
.Cells["Deposits"].Value.ToString().Length != 0)
{
deposit = int.Parse(DataGridView1.Rows[counter]
.Cells["Deposits"].Value.ToString());
}
}
if (DataGridView1.Rows[counter].Cells["Withdrawals"].Value != null)
{
if (DataGridView1.Rows[counter]
.Cells["Withdrawals"].Value.ToString().Length != 0)
{
withdrawal = int.Parse(DataGridView1.Rows[counter]
.Cells["Withdrawals"].Value.ToString());
}
}
DataGridView1.Rows[counter].Cells["Balance"].Value =
(balance + deposit + withdrawal).ToString();
}
}
Private Sub CellValueChanged(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellValueChanged
' Update the balance column whenever the values of any cell changes.
UpdateBalance()
End Sub
Private Sub RowsRemoved(ByVal sender As Object, _
ByVal e As DataGridViewRowsRemovedEventArgs) _
Handles DataGridView1.RowsRemoved
' Update the balance column whenever rows are deleted.
UpdateBalance()
End Sub
Private Sub UpdateBalance()
Dim counter As Integer
Dim balance As Integer
Dim deposit As Integer
Dim withdrawal As Integer
' Iterate through the rows, skipping the Starting Balance Row.
For counter = 1 To (DataGridView1.Rows.Count - 2)
deposit = 0
withdrawal = 0
balance = Integer.Parse(DataGridView1.Rows(counter - 1) _
.Cells("Balance").Value.ToString())
If Not DataGridView1.Rows(counter) _
.Cells("Deposits").Value Is Nothing Then
' Verify that the cell value is not an empty string.
If Not DataGridView1.Rows(counter) _
.Cells("Deposits").Value.ToString().Length = 0 Then
deposit = Integer.Parse(DataGridView1.Rows(counter) _
.Cells("Deposits").Value.ToString())
End If
End If
If Not DataGridView1.Rows(counter) _
.Cells("Withdrawals").Value Is Nothing Then
If Not DataGridView1.Rows(counter) _
.Cells("Withdrawals").Value.ToString().Length = 0 Then
withdrawal = Integer.Parse(DataGridView1.Rows(counter) _
.Cells("Withdrawals").Value.ToString())
End If
End If
DataGridView1.Rows(counter).Cells("Balance").Value = _
(balance + deposit + withdrawal).ToString()
Next
End Sub
Poznámky
K DataGridView.CellValueChanged události dochází, když je potvrzena hodnota zadaná uživatelem, která obvykle nastane, když fokus opustí buňku.
V případě buněk zaškrtávacích políček ale obvykle budete chtít změnu zpracovat okamžitě. Musíte zpracovat událost DataGridView.CurrentCellDirtyStateChanged, chcete-li potvrdit změnu po kliknutí na buňku. V obslužné rutině, pokud je aktuální buňka buňkou zaškrtávacího políčka, zavolejte metodu DataGridView.CommitEdit a předejte hodnotu Commit.
Řádky v ovládacím prvku se při změně hodnoty buňky automaticky seřadí. Pokud chcete ovládací prvek seřadit, když uživatel upraví buňku, zavolejte Sort metodu v obslužné rutině CellValueChanged události.
Další informace o zpracování událostí naleznete v tématu Zpracování a vyvolávání událostí.