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)

public int DisplayIndex { get; set; }
/** @property */
public int get_DisplayIndex ()

/** @property */
public void set_DisplayIndex (int value)

public function get DisplayIndex () : int

public function set DisplayIndex (value : int)

Not applicable.

Property Value

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.

Exception typeCondition

ArgumentOutOfRangeException

DataGridViewElement.DataGridView is not a null reference (Nothing in Visual Basic) 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 a null reference (Nothing in Visual Basic) 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 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;
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2015 Microsoft