DataGridViewBand.Index 屬性

定義

取得 DataGridView 控制項內群組列的相對位置。

public:
 property int Index { int get(); };
[System.ComponentModel.Browsable(false)]
public int Index { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Index : int
Public ReadOnly Property Index As Integer

屬性值

DataGridViewRowCollection 中的群組列之以零起始的位置,或是包含它的 DataGridViewColumnCollection。 預設值為 -1,表示沒有關聯的 DataGridView 控制項。

屬性

範例

下列程式碼範例會 Index 使用 屬性來設定資料行標籤。 透過 存取帶的 Index 屬性 DataGridViewColumn

注意

如果已啟用視覺化樣式,則目前的主題會覆寫資料列和資料行標頭的顯示樣式屬性。

// Style and number columns.
void Button8_Click( Object^ /*sender*/, EventArgs^ /*args*/ )
{
   DataGridViewCellStyle^ style = gcnew DataGridViewCellStyle;
   style->Alignment = DataGridViewContentAlignment::MiddleCenter;
   style->ForeColor = Color::IndianRed;
   style->BackColor = Color::Ivory;
   IEnumerator^ myEnum1 = dataGridView->Columns->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      DataGridViewColumn^ column = safe_cast<DataGridViewColumn^>(myEnum1->Current);
      column->HeaderCell->Value = column->Index.ToString();
      column->HeaderCell->Style = style;
   }
}
// Style and number columns.
private void Button8_Click(object sender,
    EventArgs args)
{
    DataGridViewCellStyle style = new DataGridViewCellStyle();
    style.Alignment =
        DataGridViewContentAlignment.MiddleCenter;
    style.ForeColor = Color.IndianRed;
    style.BackColor = Color.Ivory;

    foreach (DataGridViewColumn column in dataGridView.Columns)
    {
        column.HeaderCell.Value = column.Index.ToString();
        column.HeaderCell.Style = style;
    }
}
' Style and number columns.
Private Sub Button8_Click(ByVal sender As Object, _
    ByVal args As EventArgs) Handles Button8.Click

    Dim style As DataGridViewCellStyle = _
        New DataGridViewCellStyle()
    style.Alignment = _
        DataGridViewContentAlignment.MiddleCenter
    style.ForeColor = Color.IndianRed
    style.BackColor = Color.Ivory

    For Each column As DataGridViewColumn _
        In dataGridView.Columns

        column.HeaderCell.Value = _
            column.Index.ToString
        column.HeaderCell.Style = style
    Next
End Sub

備註

這個屬性的值不一定對應到集合中帶的目前視覺位置。 例如,如果使用者在執行時間重新排序 DataGridView 資料行, (假設 AllowUserToOrderColumns 屬性設定為 true) ,則每個資料行的 Index 屬性值將不會變更。 相反地,資料行 DisplayIndex 值會變更。 不過,排序資料列會變更其 Index 值。

適用於

另請參閱