Expandir Minimizar
Personas que lo han encontrado útil: 1 de 1 - Valorar este tema

DataGridView.RowValidating (Evento)

Actualización: noviembre 2007

Se produce cuando se está validando una fila.

Espacio de nombres:  System.Windows.Forms
Ensamblado:  System.Windows.Forms (en System.Windows.Forms.dll)
public event DataGridViewCellCancelEventHandler RowValidating
/** @event */
public void add_RowValidating (DataGridViewCellCancelEventHandler value)
/** @event */
public void remove_RowValidating (DataGridViewCellCancelEventHandler value)

JScript no admite eventos.

Este evento es análogo al evento Control.Validating. Úselo para realizar la validación en todos los valores de una fila. Si la fila no pasa la validación, establezca la propiedad CancelEventArgs.Cancel en true. La cancelación de este evento impide que se produzcan los eventos RowValidated, Control.Validating, y Control.Validated, y también impide que el usuario deje la fila no válida y que la fila se guarde en un origen de datos externo en el modo de enlace a datos.

Para obtener más información sobre la forma de controlar eventos, vea Utilizar eventos.

El ejemplo de código siguiente utiliza RowValidating para comprobar si se escriben 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;
    }


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
¿Te ha resultado útil?
(Caracteres restantes: 1500)

Adiciones de comunidad

AGREGAR
© 2013 Microsoft. Reservados todos los derechos.