Evaluar y enviar comentarios
MSDN
MSDN Library
 DataGridViewCellCancelEventHandler ...
Contraer todo/Expandir todo Contraer todo
Esta página es específica de
Microsoft Visual Studio 2008/.NET Framework 3.5

Hay además otras versiones disponibles para:
Biblioteca de clases de .NET Framework
DataGridViewCellCancelEventHandler (Delegado)

Actualización: noviembre 2007

Representa el método que controlará los eventos CellBeginEdit y RowValidating de DataGridView.

Espacio de nombres:  System.Windows.Forms
Ensamblado:  System.Windows.Forms (en System.Windows.Forms.dll)
Visual Basic (Declaración)
Public Delegate Sub DataGridViewCellCancelEventHandler ( _
    sender As Object, _
    e As DataGridViewCellCancelEventArgs _
)
Visual Basic (Uso)
Dim instance As New DataGridViewCellCancelEventHandler(AddressOf HandlerMethod)
C#
public delegate void DataGridViewCellCancelEventHandler(
    Object sender,
    DataGridViewCellCancelEventArgs e
)
Visual C++
public delegate void DataGridViewCellCancelEventHandler(
    Object^ sender, 
    DataGridViewCellCancelEventArgs^ e
)
J#
/** @delegate */
public delegate void DataGridViewCellCancelEventHandler(
    Object sender,
    DataGridViewCellCancelEventArgs e
)
JScript
JScript no admite delegados.

Parámetros

sender
Tipo: System..::.Object
Origen del evento.
e
Tipo: System.Windows.Forms..::.DataGridViewCellCancelEventArgs
DataGridViewCellCancelEventArgs que contiene los datos del evento.

Cuando se crea un delegado DataGridViewCellCancelEventHandler, se identifica el método que controlará el evento. Para asociarlo al controlador de eventos, se debe agregar al evento una instancia del delegado. Siempre que se produzca el evento, se llamará a su controlador, a menos que se quite el delegado. Para obtener más información sobre los delegados de los controladores de eventos, vea Eventos y delegados.

El ejemplo de código siguiente utiliza un delegado DataGridViewCellCancelEventHandler para comprobar si se escriben fechas válidas de seguimiento y lanzamiento.

Visual Basic
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
C#
    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;
    }

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

.NET Framework

Compatible con: 3.5, 3.0, 2.0
Contenido de la comunidad   ¿Qué es Community Content?
Agregar contenido nuevo RSS  Anotaciones
Processing
© 2012 Microsoft. Reservados todos los derechos. Términos de uso | Marcas Registradas | Privacidad
Page view tracker