Export (0) Print
Expand All

DataGridViewBand.Index Property

Note: This property is new in the .NET Framework version 2.0.

Gets the relative position of the band within the DataGridView control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public int Index { get; }
/** @property */
public int get_Index ()

public function get Index () : int

Property Value

The zero-based position of the band in the DataGridViewRowCollection or DataGridViewColumnCollection that it is contained within. The default is -1, indicating that there is no associated DataGridView control.

The value of this property does not necessarily correspond to the current visual position of the band within the collection. For example, if the user reorders columns in a DataGridView at run time (assuming the AllowUserToOrderColumns property is set to true), the value of the Index property of each column will not change. Instead, the column DisplayIndex values change. Sorting rows, however, does change their Index values.

The following code example uses the Index property to set column labels. The band's Index property is accessed through a DataGridViewColumn.

// Style and number columns.
private void Button8_Click(object sender,
    EventArgs args)
{
    DataGridViewCellStyle style = new DataGridViewCellStyle();
    style.Alignment =
        DataGridViewContentAlignment.MiddleCenter;
    style.ForeColor = Color.IndianRed;
    style.BackColor = Color.Ivory;

    foreach (DataGridViewColumn column in dataGridView.Columns)
    {
        column.HeaderCell.Value = column.Index.ToString();
        column.HeaderCell.Style = style;
    }
}

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft