DataGridView.CellValueChanged イベント
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As DataGridView Dim handler As DataGridViewCellEventHandler AddHandler instance.CellValueChanged, handler
public: event DataGridViewCellEventHandler^ CellValueChanged { void add (DataGridViewCellEventHandler^ value); void remove (DataGridViewCellEventHandler^ value); }

DataGridView.CellValueChanged イベントは、ユーザー指定の値がコミットされたとき (通常はフォーカスがセルを離れたとき) に発生します。
ただし、チェック ボックス セルの場合、通常は変更をすぐに処理する必要があります。セルがクリックされたときに変更をコミットするには、DataGridView.CurrentCellDirtyStateChanged イベントを処理する必要があります。ハンドラでは、現在のセルがチェック ボックス セルの場合、DataGridView.CommitEdit メソッドを呼び出して、Commit 値を渡します。
セルの値を変更しても、コントロールの行は、自動的には並べ替えられません。ユーザーがセルに変更を加えたときにコントロールを並べ替えるには、CellValueChanged イベント ハンドラで Sort メソッドを呼び出します。

CellValueChanged イベントを使用して、DataGridView の残高列の値を更新するコード例を次に示します。
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
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(); } }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DataGridView クラス
DataGridView メンバ
System.Windows.Forms 名前空間
その他の技術情報
DataGridView コントロール (Windows フォーム)
- DataGridView.CellValueChanged イベントのページへのリンク