DataGridViewCellCancelEventHandler Delegado
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Representa el método que controlará los CellBeginEdit eventos y RowValidating de .DataGridView
public delegate void DataGridViewCellCancelEventHandler(System::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)
Parámetros
- sender
- Object
Origen del evento.
que DataGridViewCellCancelEventArgs contiene los datos del evento.
Ejemplos
En el ejemplo de código siguiente se usa un DataGridViewCellCancelEventHandler delegado para comprobar si se especifican fechas válidas de seguimiento y lanzamiento.
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
Comentarios
Al crear un DataGridViewCellCancelEventHandler delegado, se identifica el método que controlará el evento. Para asociar el evento con el controlador de eventos, agregue una instancia del delegado al evento. Se llama al controlador de eventos cada vez que se produce el evento, a menos que quite el delegado. Para obtener más información sobre los delegados del controlador de eventos, consulte Control y generación de eventos.
Métodos de extensión
| Nombre | Description |
|---|---|
| GetMethodInfo(Delegate) |
Obtiene un objeto que representa el método representado por el delegado especificado. |