Export (0) Print
Expand All

DataGridViewBand.Index Property

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

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

[BrowsableAttribute(false)]
public int Index { get; }

Property Value

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

NoteNote:

If visual styles are enabled, the current theme overrides the display style properties for row and column headers.

// 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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft