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


相对于当前所显示各列,获取或设置列的显示顺序。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


与属性不同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使用GetLastColumn DataGridViewColumnCollection类的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.