DataGridViewBand.Index 属性

定义

获取带区在 DataGridView 控件中的相对位置。

C#
[System.ComponentModel.Browsable(false)]
public int Index { get; }

属性值

Int32

在包含该带区的 DataGridViewRowCollectionDataGridViewColumnCollection 中,该带区从零开始的位置。 默认值为 -1,指示没有任何关联的 DataGridView 控件。

属性

示例

下面的代码示例使用 Index 属性设置列标签。 通过 aDataGridViewColumn. 访问带Index的属性。

备注

如果启用了视觉样式,则当前主题将替代行标题和列标题的显示样式属性。

C#
// 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;
    }
}

注解

此属性的值不一定对应于集合中带的当前视觉位置。 例如,如果用户在 DataGridView 运行时对列重新排序 (假定该 AllowUserToOrderColumns 属性设置为 true) ,则每个列的属性的值 Index 不会更改。 而是更改列 DisplayIndex 值。 但是,对行进行排序会更改其 Index 值。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅