DataGridView.CellValidating 이벤트
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
셀이 입력 포커스를 잃어 콘텐츠 유효성 검사를 사용하도록 설정하면 발생합니다.
public:
event System::Windows::Forms::DataGridViewCellValidatingEventHandler ^ CellValidating;
public event System.Windows.Forms.DataGridViewCellValidatingEventHandler CellValidating;
member this.CellValidating : System.Windows.Forms.DataGridViewCellValidatingEventHandler
Public Custom Event CellValidating As DataGridViewCellValidatingEventHandler
이벤트 유형
예제
다음 코드 예제에서는 사용자가 양의 정수만 입력하도록 이벤트를 처리 CellValidating 합니다. 이 예제는 참조 항목에서 사용할 수 있는 더 큰 예제의 VirtualMode 일부입니다.
void VirtualConnector::dataGridView1_CellValidating
(Object^ sender, DataGridViewCellValidatingEventArgs^ e)
{
int newInteger;
// Don't try to validate the 'new row' until finished
// editing since there
// is not any point in validating its initial value.
if (dataGridView1->Rows[e->RowIndex]->IsNewRow)
{
return;
}
if (!Int32::TryParse(e->FormattedValue->ToString(),
newInteger) || (newInteger < 0))
{
e->Cancel = true;
}
}
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ErrorText = "";
int newInteger;
// Don't try to validate the 'new row' until finished
// editing since there
// is not any point in validating its initial value.
if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
if (!int.TryParse(e.FormattedValue.ToString(),
out newInteger) || newInteger < 0)
{
e.Cancel = true;
dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer";
}
}
Private Sub dataGridView1_CellValidating(ByVal sender As Object, _
ByVal e _
As DataGridViewCellValidatingEventArgs) _
Handles dataGridView1.CellValidating
Me.dataGridView1.Rows(e.RowIndex).ErrorText = ""
Dim newInteger As Integer
' Don't try to validate the 'new row' until finished
' editing since there
' is not any point in validating its initial value.
If dataGridView1.Rows(e.RowIndex).IsNewRow Then Return
If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
OrElse newInteger < 0 Then
e.Cancel = True
Me.dataGridView1.Rows(e.RowIndex).ErrorText = "the value must be a non-negative integer"
End If
End Sub
설명
이 이벤트를 취소하면 현재 셀의 변경 내용이 취소됩니다. 이 이벤트가 데이터 바인딩 모드에서 취소되면 새 값이 기본 데이터 원본으로 푸시되지 않습니다. 이 이벤트가 가상 모드 CellValuePushed 에서 취소되면 이벤트가 발생하지 않습니다.
CellValidated 사후 유효성 검사 처리를 수행하기 위해 이벤트를 처리합니다.
이벤트를 처리하는 방법에 대한 자세한 내용은 이벤트 처리 및 발생을 참조하세요.