DataGridViewCellCancelEventHandler 代理人
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示處理 CellBeginEdit 的 RowValidating 和 DataGridView 事件的方法。
public delegate void DataGridViewCellCancelEventHandler(System::Object ^ sender, DataGridViewCellCancelEventArgs ^ e);
public delegate void DataGridViewCellCancelEventHandler(object sender, DataGridViewCellCancelEventArgs e);
public delegate void DataGridViewCellCancelEventHandler(object? sender, DataGridViewCellCancelEventArgs e);
type DataGridViewCellCancelEventHandler = delegate of obj * DataGridViewCellCancelEventArgs -> unit
Public Delegate Sub DataGridViewCellCancelEventHandler(sender As Object, e As DataGridViewCellCancelEventArgs)
參數
- sender
- Object
事件的來源。
範例
下列程式碼範例會使用 DataGridViewCellCancelEventHandler 委派來檢查是否已輸入有效的追蹤和發行日期。
private void ValidateByRow(Object sender,
DataGridViewCellCancelEventArgs data)
{
DataGridViewRow row =
songsDataGridView.Rows[data.RowIndex];
DataGridViewCell trackCell =
row.Cells[songsDataGridView.Columns["Track"].Index];
DataGridViewCell dateCell =
row.Cells[songsDataGridView.Columns["Release Date"].Index];
data.Cancel = !(IsTrackGood(trackCell) && IsDateGood(dateCell));
}
private Boolean IsTrackGood(DataGridViewCell cell)
{
Int32 cellValueAsInt;
if (cell.Value.ToString().Length == 0)
{
cell.ErrorText = "Please enter a track";
songsDataGridView.Rows[cell.RowIndex].ErrorText =
"Please enter a track";
return false;
}
else if (cell.Value.ToString().Equals("0"))
{
cell.ErrorText = "Zero is not a valid track";
songsDataGridView.Rows[cell.RowIndex].ErrorText =
"Zero is not a valid track";
return false;
}
else if (!Int32.TryParse(cell.Value.ToString(), out cellValueAsInt))
{
cell.ErrorText = "A Track must be a number";
songsDataGridView.Rows[cell.RowIndex].ErrorText =
"A Track must be a number";
return false;
}
return true;
}
private Boolean IsDateGood(DataGridViewCell cell)
{
if (cell.Value == null)
{
cell.ErrorText = "Missing date";
songsDataGridView.Rows[cell.RowIndex].ErrorText =
"Missing date";
return false;
}
else
{
try
{
DateTime.Parse(cell.Value.ToString());
}
catch (FormatException)
{
cell.ErrorText = "Invalid format";
songsDataGridView.Rows[cell.RowIndex].ErrorText =
"Invalid format";
return false;
}
}
return true;
}
Private Sub ValidateByRow(ByVal sender As Object, _
ByVal data As DataGridViewCellCancelEventArgs) _
Handles songsDataGridView.RowValidating
Dim row As DataGridViewRow = _
songsDataGridView.Rows(data.RowIndex)
Dim trackCell As DataGridViewCell = _
row.Cells(songsDataGridView.Columns("Track").Index)
Dim dateCell As DataGridViewCell = _
row.Cells(songsDataGridView.Columns("Release Date").Index)
data.Cancel = Not (IsTrackGood(trackCell) _
AndAlso IsDateGood(dateCell))
End Sub
Private Function IsTrackGood(ByRef cell As DataGridViewCell) As Boolean
If cell.Value.ToString().Length = 0 Then
cell.ErrorText = "Please enter a track"
songsDataGridView.Rows(cell.RowIndex).ErrorText = _
"Please enter a track"
Return False
ElseIf cell.Value.ToString().Equals("0") Then
cell.ErrorText = "Zero is not a valid track"
songsDataGridView.Rows(cell.RowIndex).ErrorText = _
"Zero is not a valid track"
Return False
ElseIf Not Integer.TryParse( _
cell.Value.ToString(), New Integer()) Then
cell.ErrorText = "A Track must be a number"
songsDataGridView.Rows(cell.RowIndex).ErrorText = _
"A Track must be a number"
Return False
End If
Return True
End Function
Private Function IsDateGood(ByRef cell As DataGridViewCell) As Boolean
If cell.Value Is Nothing Then
cell.ErrorText = "Missing date"
songsDataGridView.Rows(cell.RowIndex).ErrorText = _
"Missing date"
Return False
Else
Try
DateTime.Parse(cell.Value.ToString())
Catch ex As FormatException
cell.ErrorText = "Invalid format"
songsDataGridView.Rows(cell.RowIndex).ErrorText = _
"Invalid format"
Return False
End Try
End If
Return True
End Function
備註
建立 DataGridViewCellCancelEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 處理和引發事件。
擴充方法
GetMethodInfo(Delegate) |
取得表示特定委派所代表之方法的物件。 |