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

注解

此属性的值不一定对应于集合中的带区当前 visual 位置。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.

适用于

另请参阅