Export (0) Print
Expand All

DataGridViewColumn.DisplayIndex Property

Gets or sets the display order of the column relative to the currently displayed columns.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
<BrowsableAttribute(False)> _
Public Property DisplayIndex As Integer

Property Value

Type: System.Int32
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.

ExceptionCondition
ArgumentOutOfRangeException

DataGridViewElement.DataGridView is not Nothing 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-

DataGridViewElement.DataGridView is Nothing and the specified value when setting this property is less than -1.

-or-

The specified value when setting this property is equal to Int32.MaxValue.

Unlike the Index property, the DisplayIndex property corresponds to the current position of the column as displayed by the user interface (UI). By default, each column's DisplayIndex is set to numbers of increasing order, which reflects the order in which they were added. The Visible property value does not affect the DisplayIndex value. 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.

Every column in the control has a unique DisplayIndex value. The values start with 0 and proceed in numerical order without skipping any values. When you change the DisplayIndex value for a column, the DisplayIndex values for other columns are changed to reflect the new order.

If the column has an associated DataGridView control, setting this property will cause the control to redraw itself.

The following code example uses the DisplayIndex property to swap the visual position of the first and last columns. Note that insertions occur before the DisplayIndex. This code example is part of a larger example provided for the DataGridViewColumn class.


' 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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft