DataGridViewBand.Tag Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[BrowsableAttribute(false)]
public object Tag { get; set; }

Property Value

Type: System.Object

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

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.

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

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

.NET Framework
Available since 2.0
Return to top
Show: