DataGridViewBand.Index DataGridViewBand.Index DataGridViewBand.Index DataGridViewBand.Index Property

定义

获取带区在 DataGridView 控件中的相对位置。Gets the relative position of the band within the DataGridView control.

public:
 property int Index { int get(); };
[System.ComponentModel.Browsable(false)]
public int Index { get; }
member this.Index : int
Public ReadOnly Property Index As Integer

属性值

在包含该带区的 DataGridViewRowCollectionDataGridViewColumnCollection 中,该带区从零开始的位置。The zero-based position of the band in the DataGridViewRowCollection or DataGridViewColumnCollection that it is contained within. 默认值为 -1,指示没有任何关联的 DataGridView 控件。The default is -1, indicating that there is no associated DataGridView control.

示例

下面的代码示例使用Index属性设置列标签。The following code example uses the Index property to set column labels. 通过访问带Index区的属性。 DataGridViewColumnThe band's Index property is accessed through a DataGridViewColumn.

备注

如果启用了视觉样式, 则当前主题将覆盖行标题和列标题的显示样式属性。If visual styles are enabled, the current theme overrides the display style properties for row and column headers.

// Style and number columns.
void Button8_Click( Object^ /*sender*/, EventArgs^ /*args*/ )
{
   DataGridViewCellStyle^ style = gcnew DataGridViewCellStyle;
   style->Alignment = DataGridViewContentAlignment::MiddleCenter;
   style->ForeColor = Color::IndianRed;
   style->BackColor = Color::Ivory;
   IEnumerator^ myEnum1 = dataGridView->Columns->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      DataGridViewColumn^ column = safe_cast<DataGridViewColumn^>(myEnum1->Current);
      column->HeaderCell->Value = column->Index.ToString();
      column->HeaderCell->Style = style;
   }
}


// 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;
    }
}
' Style and number columns.
Private Sub Button8_Click(ByVal sender As Object, _
    ByVal args As EventArgs) Handles Button8.Click

    Dim style As DataGridViewCellStyle = _
        New DataGridViewCellStyle()
    style.Alignment = _
        DataGridViewContentAlignment.MiddleCenter
    style.ForeColor = Color.IndianRed
    style.BackColor = Color.Ivory

    For Each column As DataGridViewColumn _
        In dataGridView.Columns

        column.HeaderCell.Value = _
            column.Index.ToString
        column.HeaderCell.Style = style
    Next
End Sub

注解

此属性的值不一定与集合中带区的当前可视位置相对应。The value of this property does not necessarily correspond to the current visual position of the band within the collection. 例如, 如果用户在DataGridView运行时对列进行重新排序 ( AllowUserToOrderColumns假设属性设置为true), 则每个列的Index属性的值将不会更改。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. 相反, 列值DisplayIndex会更改。Instead, the column DisplayIndex values change. 但对行进行排序时, 会Index更改其值。Sorting rows, however, does change their Index values.

适用于

另请参阅