datagridview 資料修改後,發生於儲存格的狀態隨著其內容中的變更而一起變更時的事件

'Ref. http://msdn.microsoft.com/zh-tw/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tmpTB As New DataTable
        With tmpTB
            .Columns.Add("欄一")
            .Columns.Add("欄二")
            For i As Integer = 0 To 10
                Dim newRow As DataRow = .NewRow()
                newRow("欄一") = i
                newRow("欄二") = i + 11
                .Rows.Add(newRow)
            Next
            DataGridView1.DataSource = tmpTB
        End With
        Dim NewColumn As New DataGridViewCheckBoxColumn
        DataGridView1.Columns.Insert(0, NewColumn)
    End Sub
    'datagridview 資料修改後,發生於儲存格的狀態隨著其內容中的變更而一起變更時的事件
    Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
        If e.ColumnIndex = 0 Then Me.Text = DataGridView1.CurrentRow.Cells("欄二").Value
    End Sub
    'Ref. http://msdn.microsoft.com/zh-tw/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx
    Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
        If DataGridView1.IsCurrentCellDirty Then DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End Sub
End Class

沒有留言:

張貼留言