DataGridViewCellValidatingEventArgs 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供控制 CellValidating 事件 DataGridView 的資料。
public ref class DataGridViewCellValidatingEventArgs : System::ComponentModel::CancelEventArgs
public class DataGridViewCellValidatingEventArgs : System.ComponentModel.CancelEventArgs
type DataGridViewCellValidatingEventArgs = class
inherit CancelEventArgs
Public Class DataGridViewCellValidatingEventArgs
Inherits CancelEventArgs
- 繼承
範例
以下程式碼範例處理 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
備註
這個 DataGridView.CellValidating 事件讓你在新值無效時取消對當前儲存格的變更。 利用該 FormattedValue 物業來判斷當前價值。 要判斷當前儲存格的狀態,請使用 和 RowIndexColumnIndex 屬性透過 DataGridView.Rows 集合存取儲存格。 要取消該變更,請將屬性設定 Cancel 為 true。
當此事件在資料綁定模式下被取消時,新的值不會推送到底層資料來源。 當此活動在虛擬模式中被取消時,活動 DataGridView.CellValuePushed 將不會被提出。
屬性
| 名稱 | Description |
|---|---|
| Cancel |
取得或設定一個值,指示事件是否應被取消。 (繼承來源 CancelEventArgs) |
| ColumnIndex |
取得需要驗證的儲存格的欄位索引。 |
| FormattedValue |
取得需要驗證的儲存格格式內容。 |
| RowIndex |
取得需要驗證的儲存格的列索引。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |