DataGridViewBand.Index 屬性


取得 DataGridView 控制項內群組列的相對位置。Gets the relative position of the band within the DataGridView control.

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



DataGridViewRowCollection 中的群組列之以零起始的位置,或是包含它的 DataGridViewColumnCollectionThe zero-based position of the band in the DataGridViewRowCollection or DataGridViewColumnCollection that it is contained within. 預設值為 -1,表示沒有關聯的 DataGridView 控制項。The default is -1, indicating that there is no associated DataGridView control.



下列程式碼範例會使用 Index 屬性來設定資料行標籤。The following code example uses the Index property to set column labels. Index 可以透過來存取此寬線的屬性 DataGridViewColumnThe band's Index property is accessed through a DataGridViewColumn.


如果啟用視覺化樣式,則目前的主題會覆寫資料列和資料行標頭的顯示樣式屬性。If visual styles are enabled, the current theme overrides the display style properties for row and column headers.

// 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 =
    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 = _
    style.ForeColor = Color.IndianRed
    style.BackColor = Color.Ivory

    For Each column As DataGridViewColumn _
        In dataGridView.Columns

        column.HeaderCell.Value = _
        column.HeaderCell.Style = style
End Sub


這個屬性的值不一定會對應到集合中的目前範圍視覺位置。The value of this property does not necessarily correspond to the current visual position of the band within the collection. 例如,如果使用者在執行時間重新排序資料行 DataGridView (假設 AllowUserToOrderColumns 屬性設定為 true) ,則 Index 每個資料行的屬性值都不會變更。For example, if the user reorders columns in a DataGridView at run time (assuming the AllowUserToOrderColumns property is set to true), the value of the Index property of each column will not change. 相反地, DisplayIndex 資料行值會變更。Instead, the column DisplayIndex values change. 但是,排序資料列的確會變更其 Index 值。Sorting rows, however, does change their Index values.