DataGridViewBand::Tag Property
.NET Framework (current version)
Gets or sets the object that contains data to associate with the band.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
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.
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); } } }
.NET Framework
Available since 2.0
Available since 2.0
Show: