[VB.NET] Lỗi phím Esc để thoát chỉnh sửa trong DataGridView (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

Thanh Bình PV

Thành viên hoạt động
Tham gia
30/10/19
Bài viết
151
Được thích
19
Em chào mọi người,
Em có 1 DataGridView khi mở lên thì đã có dữ liệu sẵn.
1. Em muốn khi thay đổi dữ liệu trong 1 ô bất kỳ thì sẽ kiểm tra dữ liệu đó đã tồn tại chưa. Nếu đã tồn tại rồi thì thông báo và không thoát chỉnh sửa. (Em đã hoàn thành)
2. Nhưng em bắt sự kiện CurrentCellDirtyStateChanged thì khi em sử dụng phím Esc để hủy bỏ, nó sẽ lưu dữ liệu mới thay đổi luôn dù đã tồn tại (không trở lại dữ liệu cũ). Mà nếu xóa sự kiện CurrentCellDirtyStateChanged thì sự kiện CellValidating của em không kiểm tra được giá trị đó đã tồn tại hay chưa.
2246cf092a24d07a8935.png
Anh chị nào hỗ trợ em vấn đề này với ạ. Làm sao để vẫn kiểm tra được dữ liệu nhưng khi nhấn phím Esc để hủy bỏ thì không thay đổi dữ liệu cũ.
Em cảm ơn.
**Sự kiện CurrentCellDirtyStateChanged
Mã:
Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
        DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End Sub

**Sự kiện CellValidating
Mã:
Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
        Dim i As Integer = 0
        Dim tencot As String = "NameBodies"
        If DataGridView1.Rows(e.RowIndex).Cells(tencot).Value Is Nothing Then
            e.Cancel = True
            MessageBox.Show("Please type name.")
        Else
            Do While (i < DataGridView1.Rows.Count)
                If (Not (DataGridView1.Rows(i).Cells(tencot).Value) Is Nothing) Then
                    If (i <> e.RowIndex) AndAlso (DataGridView1.Rows(e.RowIndex).Cells(tencot).Value.ToString() = DataGridView1.Rows(i).Cells(tencot).Value.ToString()) Then
                        e.Cancel = True
                        MessageBox.Show("This name exist already. please type another")
                    End If
                End If
                i = (i + 1)
            Loop
        End If
    End Sub
 
bác nào giúp em vấn đề này được không ạ. Em tìm mấy nay rồi mà vẫn không ra.
 
Web KT

Bài viết mới nhất

Back
Top Bottom