DataGridViewCellValidatingEventArgs.RowIndex プロパティ

定義

検証する必要のあるセルの行インデックスを取得します。

public:
 property int RowIndex { int get(); };
public int RowIndex { get; }
member this.RowIndex : int
Public ReadOnly Property RowIndex As Integer

プロパティ値

検証する必要のあるセルの行インデックスを指定する、0 から始まる整数。

次のコード例は、このプロパティの使用方法を示しています。 この例は、「方法: Windows フォーム DataGridView コントロールのデータを検証する」で使用できる大きな例の一部です。

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;
}
Private Sub dataGridView1_CellValidating(ByVal sender As Object, _
    ByVal e As DataGridViewCellValidatingEventArgs) _
    Handles dataGridView1.CellValidating

    Dim headerText As String = _
        dataGridView1.Columns(e.ColumnIndex).HeaderText

    ' Abort validation if cell is not in the CompanyName column.
    If Not headerText.Equals("CompanyName") Then Return

    ' Confirm that the cell is not empty.
    If (String.IsNullOrEmpty(e.FormattedValue.ToString())) Then
        dataGridView1.Rows(e.RowIndex).ErrorText = _
            "Company Name must not be empty"
        e.Cancel = True
    End If
End Sub

Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
    Handles dataGridView1.CellEndEdit

    ' Clear the row error in case the user presses ESC.   
    dataGridView1.Rows(e.RowIndex).ErrorText = String.Empty

End Sub

適用対象

こちらもご覧ください