DataGridViewBand.Index DataGridViewBand.Index DataGridViewBand.Index DataGridViewBand.Index Property

定義

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

public:
 property int Index { int get(); };
[System.ComponentModel.Browsable(false)]
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 =
        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

備註

這個屬性的值不會不一定會對應至目前的視覺位置,集合內群組列。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.

適用於

另請參閱