사용자가 입력 한 셀 값의 값을 변경하고 유효성 검사 전에 값의 왼쪽 및 오른쪽 공백을 피하기 위해 먼저 트리밍을 수행하고 싶습니다. 현재이이 기능없이 내 코드입니다 :CellValidating에서 datagriview 셀 값을 변경하십시오.
Private Sub Grid_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles Grid.CellValidating
Dim newValue = e.FormattedValue.ToString
If Not Grid.Rows(e.RowIndex).IsNewRow Then 'And Not e.RowIndex = Grid.NewRowIndex - 1 Then
Select Case e.ColumnIndex
Case 1 'Name
If String.IsNullOrEmpty(newValue) Then
e.Cancel = True
Exit Select
End If
Case Else
End Select
End If
End Sub
나는이 방법이었다하려고 노력 무슨 일이 있지만 완전하게 작동하지 않습니다 잘못된 이벤트의
Private Sub Grid_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles Grid.CellValidating
Dim newValue = e.FormattedValue.ToString.Trim '<---trim added
'change value of cell without trims
Dim aaa As DataGridViewCell = CType(sender, DataGridView).Rows(e.RowIndex).Cells(e.ColumnIndex)
aaa.Value = newValue
If Not Grid.Rows(e.RowIndex).IsNewRow Then 'And Not e.RowIndex = Grid.NewRowIndex - 1 Then
Select Case e.ColumnIndex
Case 1 'Name
If String.IsNullOrEmpty(newValue) Then
e.Cancel = True
Exit Select
End If
Case Else
End Select
End If
End Sub
'하지만 완전히 작동하지 않습니다. '- 작동하지 않습니까? 코드를 단계별로 디버깅 할 때 어떤 공통점을 얻었습니까? Trim 전후에 어떤 값이 e.FormattedValue.ToString입니까? –
@Alex B. 올바른 값을주고 트리밍 한 후에 오른쪽 트리밍을하기 전에. 이 말이 맞습니다. 이제는 세포에 맞추기 위해 그 값을 어떻게 저장해야할지 모르겠습니다. –
@jmcilhinney는 트리밍 코드를'CellValidated' 이벤트 핸들러에 넣고'CellValidating' 이벤트 핸들러에 IsNullOrEmpty 체크를 남겨 둡니다. –