Share via


DataGridViewBand.Index Properti

Definisi

Mendapatkan posisi relatif pita dalam DataGridView kontrol.

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

Nilai Properti

Posisi pita berbasis nol di atau DataGridViewColumnCollection yang terkandung di DataGridViewRowCollection dalamnya. Defaultnya adalah -1, menunjukkan bahwa tidak ada kontrol terkait DataGridView .

Atribut

Contoh

Contoh kode berikut menggunakan Index properti untuk mengatur label kolom. Properti band Index diakses melalui DataGridViewColumn.

Catatan

Jika gaya visual diaktifkan, tema saat ini akan menggantikan properti gaya tampilan untuk header baris dan kolom.

// 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

Keterangan

Nilai properti ini tidak selalu sesuai dengan posisi visual pita saat ini dalam koleksi. Misalnya, jika pengguna menyusun ulang kolom dalam DataGridView durasi (dengan asumsi AllowUserToOrderColumns properti diatur ke true), nilai Index properti setiap kolom tidak akan berubah. Sebagai gantinya, nilai kolom DisplayIndex berubah. Namun, mengurutkan baris, mengubah nilainya Index .

Berlaku untuk

Lihat juga