Export (0) Print
Expand All
Expand Minimize
0 out of 2 rated this helpful - Rate this topic

DataGridView.RowValidating Event

Occurs when a row is validating.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public event DataGridViewCellCancelEventHandler RowValidating

This event is analogous to the Control.Validating event. Use this event to perform validation on all the values of a row. If the row does not pass validation, set the CancelEventArgs.Cancel property to true. Canceling this event prevents the RowValidated, Control.Validating, and Control.Validated events from occurring, and also prevents the user from leaving the invalid row and prevents the row from being saved to an external data source in data bound mode.

For more information about handling events, see Consuming Events.

The following code example uses RowValidating to check if valid track and release dates are entered.

    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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.