DataGridViewBand.Tag プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

バンドに関連付けられた情報を含む Object。既定値は null 参照 (Visual Basic では Nothing) です。

Tag プロパティには、バンドに関連付ける必要のある任意のオブジェクトを格納できます。このプロパティは通常、文字列名、一意の識別子 (Guid など)、データベース内のバンドのデータのインデックスなどの識別情報を格納するために使用されます。

Tag プロパティを使用して色を格納し、これを取得して BackColor プロパティを設定するコード例を次に示します。
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 Not band.Tag Is Nothing Then band.DefaultCellStyle.BackColor = _ CType(band.Tag, Color) End If Next For Each band As DataGridViewBand In dataGridView.Rows If Not band.Tag Is Nothing Then band.DefaultCellStyle.BackColor = _ CType(band.Tag, Color) End If Next End Sub
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; } } }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- DataGridViewBand.Tag プロパティのページへのリンク