DataGridViewColumn.DisplayIndex 屬性


取得或設定相對於目前所顯示之資料行的資料行顯示順序。Gets or sets the display order of the column relative to the currently displayed columns.

 property int DisplayIndex { int get(); void set(int value); };
public int DisplayIndex { get; set; }
member this.DisplayIndex : int with get, set
Public Property DisplayIndex As Integer


顯示在相關聯 DataGridView 中之資料行的以零起始位置,或是如果群組列並未包含在控制項之內,則為 -1。The zero-based position of the column as it is displayed in the associated DataGridView, or -1 if the band is not contained within a control.



DataGridView 不是 null,而且設定這個屬性時,指定的值小於 0,或是大於或等於控制項中資料行數。DataGridView is not null and the specified value when setting this property is less than 0 or greater than or equal to the number of columns in the control.

-或--or- DataGridViewnull,而且設定這個屬性時,指定的值小於 -1。DataGridView is null and the specified value when setting this property is less than -1.

-或--or- 在設定這個屬性時,所指定的值等於 MaxValueThe specified value when setting this property is equal to MaxValue.


下列程式碼範例會使用 DisplayIndex 屬性來交換第一個和最後一個資料行的視覺位置。The following code example uses the DisplayIndex property to swap the visual position of the first and last columns. 請注意,插入會在 DisplayIndex之前進行。Note that insertions occur before the DisplayIndex. 這個程式碼範例是針對 DataGridViewColumn 類別所提供的較大範例的一部分。This code example is part of a larger example provided for the DataGridViewColumn class.

// Swap the last column with the first.
void Button10_Click( Object^ /*sender*/, EventArgs^ /*args*/ )
   DataGridViewColumnCollection^ columnCollection = dataGridView->Columns;
   DataGridViewColumn^ firstDisplayedColumn = columnCollection->GetFirstColumn( DataGridViewElementStates::Visible );
   DataGridViewColumn^ lastDisplayedColumn = columnCollection->GetLastColumn( DataGridViewElementStates::Visible, DataGridViewElementStates::None );
   int firstColumn_sIndex = firstDisplayedColumn->DisplayIndex;
   firstDisplayedColumn->DisplayIndex = lastDisplayedColumn->DisplayIndex;
   lastDisplayedColumn->DisplayIndex = firstColumn_sIndex;

// Swap the last column with the first.
private void Button10_Click(object sender, EventArgs args)
    DataGridViewColumnCollection columnCollection = dataGridView.Columns;

    DataGridViewColumn firstVisibleColumn =
    DataGridViewColumn lastVisibleColumn =
            DataGridViewElementStates.Visible, DataGridViewElementStates.None);

    int firstColumn_sIndex = firstVisibleColumn.DisplayIndex;
    firstVisibleColumn.DisplayIndex = lastVisibleColumn.DisplayIndex;
    lastVisibleColumn.DisplayIndex = firstColumn_sIndex;
' Swap the last column with the first.
Private Sub Button10_Click(ByVal sender As Object, _
    ByVal args As EventArgs) Handles Button10.Click

    Dim columnCollection As DataGridViewColumnCollection = _

    Dim firstVisibleColumn As DataGridViewColumn = _
    Dim lastVisibleColumn As DataGridViewColumn = _
        columnCollection.GetLastColumn(DataGridViewElementStates.Visible, _

    Dim firstColumn_sIndex As Integer = firstVisibleColumn.DisplayIndex
    firstVisibleColumn.DisplayIndex = _
    lastVisibleColumn.DisplayIndex = firstColumn_sIndex
End Sub


Index 屬性不同的是,DisplayIndex 屬性會對應至使用者介面(UI)所顯示的資料行目前位置。Unlike the Index property, the DisplayIndex property corresponds to the current position of the column as displayed by the user interface (UI). 根據預設,每個資料行的 DisplayIndex 都會設定為遞增順序的數目,而這會反映加入它們的順序。By default, each column's DisplayIndex is set to numbers of increasing order, which reflects the order in which they were added. Visible 屬性值不會影響 DisplayIndex 值。The Visible property value does not affect the DisplayIndex value. 若要根據可見度或其他狀態判斷資料行的顯示位置,請使用 DataGridViewColumnCollection 類別的 [GetFirstColumn]、[GetLastColumn] 或 [GetNextColumn] 方法。To determine the display position of a column based on its visibility or other state, use the GetFirstColumn, GetLastColumn, or GetNextColumn method of the DataGridViewColumnCollection class.

控制項中的每個資料行都有唯一的 DisplayIndex 值。Every column in the control has a unique DisplayIndex value. 值的開頭為0,並以數位順序繼續,而不會略過任何值。The values start with 0 and proceed in numerical order without skipping any values. 當您變更資料行的 DisplayIndex 值時,其他資料行的 DisplayIndex 值會變更,以反映新的順序。When you change the DisplayIndex value for a column, the DisplayIndex values for other columns are changed to reflect the new order.

如果資料行有相關聯的 DataGridView 控制項,則設定這個屬性將會導致控制項自行重繪。If the column has an associated DataGridView control, setting this property will cause the control to redraw itself.