DataGridViewColumn.DisplayIndex DataGridViewColumn.DisplayIndex DataGridViewColumn.DisplayIndex DataGridViewColumn.DisplayIndex Property

定義

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

public:
 property int DisplayIndex { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
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 =
        columnCollection.GetFirstColumn(DataGridViewElementStates.Visible);
    DataGridViewColumn lastVisibleColumn =
        columnCollection.GetLastColumn(
            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 = _
        dataGridView.Columns

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

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

備註

與屬性不同的是DisplayIndex ,屬性會對應至使用者介面(UI)所顯示的資料行目前位置。 IndexUnlike 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. 屬性值不DisplayIndex會影響值。 VisibleThe Visible property value does not affect the DisplayIndex value. 若要根據可見度或其他狀態判斷資料行的顯示位置, GetFirstColumn請使用DataGridViewColumnCollection類別的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.

適用於

另請參閱