DataGridViewBand.Tag Property

Definition

Gets or sets the object that contains data to associate with the band.

C#
[System.ComponentModel.Browsable(false)]
public object Tag { get; set; }
C#
[System.ComponentModel.Browsable(false)]
public object? Tag { get; set; }

Property Value

An Object that contains information associated with the band. The default is null.

Attributes

Examples

The following code example uses the Tag property to store a color that is later retrieved to set the BackColor property.

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

Remarks

The Tag property can store any object that you want to associate with a band. This property is typically used to store identifying information, such as a string name, a unique identifier (for example, a Guid), or the index of the band's data in a database.

Applies to

Product Versions
.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, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also