DataGridViewCellValidatingEventArgs.ColumnIndex 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得需要驗證的儲存格之資料行索引。
public:
property int ColumnIndex { int get(); };
public int ColumnIndex { get; }
member this.ColumnIndex : int
Public ReadOnly Property ColumnIndex As Integer
屬性值
以零起始的整數,指定需要驗證的儲存格之資料行索引。
範例
下列程式碼範例說明此屬性的使用。 此範例是如何:驗證 DataGridView 控制項中Windows Forms資料中較大範例的一部分。
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