RowIndex Property
Collapse the table of content
Expand the table of content

DataGridViewCellValidatingEventArgs.RowIndex Property

 

Gets the row index of the cell that needs to be validated.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public int RowIndex { get; }

Property Value

Type: System.Int32

A zero-based integer that specifies the row index of the cell that needs to be validated.

The following code example illustrates the use of this property. This example is part of a larger example available in How to: Validate Data in the Windows Forms DataGridView Control.

private void dataGridView1_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
{
    string headerText = 
        dataGridView1.Columns[e.ColumnIndex].HeaderText;

    // Abort validation if cell is not in the CompanyName column.
    if (!headerText.Equals("CompanyName")) return;

    // Confirm that the cell is not empty.
    if (string.IsNullOrEmpty(e.FormattedValue.ToString()))
    {
        dataGridView1.Rows[e.RowIndex].ErrorText =
            "Company Name must not be empty";
        e.Cancel = true;
    }
}

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    // Clear the row error in case the user presses ESC.   
    dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
}

.NET Framework
Available since 2.0
Return to top
Show:
© 2016 Microsoft