DataGridViewBand.Tag 属性

定义

获取或设置包含与带区关联的数据的对象。

public:
 property System::Object ^ Tag { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.Browsable(false)]
public object Tag { get; set; }
[System.ComponentModel.Browsable(false)]
public object? Tag { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.Tag : obj with get, set
Public Property Tag As Object

属性值

Object

一个 Object,它包含与带区关联的信息。 默认值为 null

属性

示例

下面的代码示例使用 Tag 该属性来存储稍后检索到以设置 BackColor 属性的颜色。

void PostRowCreation()
{
   SetBandColor( dataGridView->Columns[ 0 ], Color::CadetBlue );
   SetBandColor( dataGridView->Rows[ 1 ], Color::Coral );
   SetBandColor( dataGridView->Columns[ 2 ], Color::DodgerBlue );
}

void SetBandColor( DataGridViewBand^ band, Color color )
{
   band->Tag = color;
}


// Color the bands by the value stored in their tag.
void Button9_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   IEnumerator^ myEnum1 = dataGridView->Columns->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      DataGridViewBand^ band = static_cast<DataGridViewBand^>(myEnum1->Current);
      if ( band->Tag != nullptr )
      {
         band->DefaultCellStyle->BackColor =  *dynamic_cast<Color^>(band->Tag);
      }
   }

   IEnumerator^ myEnum2 = safe_cast<IEnumerable^>(dataGridView->Rows)->GetEnumerator();
   while ( myEnum2->MoveNext() )
   {
      DataGridViewBand^ band = safe_cast<DataGridViewBand^>(myEnum2->Current);
      if ( band->Tag != nullptr )
      {
         band->DefaultCellStyle->BackColor =  *dynamic_cast<Color^>(band->Tag);
      }
   }
}
private void PostRowCreation()
{
    SetBandColor(dataGridView.Columns[0], Color.CadetBlue);
    SetBandColor(dataGridView.Rows[1], Color.Coral);
    SetBandColor(dataGridView.Columns[2], Color.DodgerBlue);
}

private static void SetBandColor(DataGridViewBand band, Color color)
{
    band.Tag = color;
}

// Color the bands by the value stored in their tag.
private void Button9_Click(object sender, System.EventArgs e)
{

    foreach (DataGridViewBand band in dataGridView.Columns)
    {
        if (band.Tag != null)
        {
            band.DefaultCellStyle.BackColor = (Color)band.Tag;
        }
    }

    foreach (DataGridViewBand band in dataGridView.Rows)
    {
        if (band.Tag != null)
        {
            band.DefaultCellStyle.BackColor = (Color)band.Tag;
        }
    }
}
Private Sub PostRowCreation()
    SetBandColor(dataGridView.Columns(0), Color.CadetBlue)
    SetBandColor(dataGridView.Rows(1), Color.Coral)
    SetBandColor(dataGridView.Columns(2), Color.DodgerBlue)
End Sub

Private Shared Sub SetBandColor(ByVal band As DataGridViewBand, _
    ByVal color As Color)
    band.Tag = color
End Sub

' Color the bands by the value stored in their tag.
Private Sub Button9_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button9.Click

    For Each band As DataGridViewBand In dataGridView.Columns
        If band.Tag IsNot Nothing Then
            band.DefaultCellStyle.BackColor = _
                CType(band.Tag, Color)
        End If
    Next

    For Each band As DataGridViewBand In dataGridView.Rows
        If band.Tag IsNot Nothing Then
            band.DefaultCellStyle.BackColor = _
                CType(band.Tag, Color)
        End If
    Next
End Sub

注解

Tag 属性可以存储要与带关联的任何对象。 此属性通常用于存储标识信息,例如字符串名称、唯一标识符 (,例如 Guid) 或数据库中带数据的索引。

适用于

另请参阅