VB.NET, C#, PowerShellを使用しているエンジニアのためのサイト。
基本的な使用方法から開発で役立つTipsまで幅広く取り扱っています。
DataGridの列ごとに背景色と前景色を設定できるメソッドやプロパティを探したのですが見つけることができなかったためクラスを自作しました。
列ごとのスタイルを設定する場合は通常DataGridTextBoxColumnやDataGridBoolColumnを使用します。 DataGridTextBoxColumnからの派生クラスDataGridTextColumnColorと、DataGridBoolColumn からの派生クラスDataGridBoolColumnColorを紹介します。
それぞれのクラスにCellBackColorプロパティ、CellForeColorプロパティを作成し、背景色と前景色をセットできるようにしています。プロパティにセットした値はオーバーライドしたPaintメソッドで使用し、列の背景色と前景色を変更しています。
'DataGridTextColmunColor クラス Imports System Imports System.Windows.Forms Imports System.Drawing 'DataGridTextColmunColor クラス Public Class DataGridTextColumnColor Inherits DataGridTextBoxColumn 'メンバ変数の宣言 Dim _CellBackColor As System.Drawing.Color Dim _CellForeColor As System.Drawing.Color 'コンストラクタ Public Sub New() '背景色の初期値を白にセット _CellBackColor = System.Drawing.Color.White '前景色の初期値を黒にセット _CellForeColor = System.Drawing.Color.Black End Sub '列の背景色を設定取得します Public Property CellBackColor() As Color Get Return _CellBackColor End Get Set(ByVal Value As Color) _CellBackColor = Value End Set End Property '列の前景色を設定取得します Public Property CellForeColor() As Color Get Return _CellForeColor End Get Set(ByVal Value As Color) _CellForeColor = Value End Set End Property 'Paintメソッドをオーバーライドします Protected Overloads Overrides Sub Paint( _ ByVal g As Graphics, _ ByVal bounds As Rectangle, _ ByVal source As CurrencyManager, _ ByVal rowNum As Integer, _ ByVal backBrush As Brush, _ ByVal foreBrush As Brush, _ ByVal alignToRight As Boolean _ ) 'セルの値を取得します Dim cellValue As Object = MyBase.GetColumnValueAtRow(source, rowNum) '前景色(文字)をセットします foreBrush = New SolidBrush(_CellForeColor) '背景色をセットします backBrush = New SolidBrush(_CellBackColor) '基本クラスのPaintメソッドを呼び出します MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight) End Sub End Class 'DataGridBoolColmunColor クラス Imports System Imports System.Windows.Forms Imports System.Drawing 'DataGridBoolColmunColor クラス Public Class DataGridBoolColumnColor Inherits DataGridBoolColumn 'メンバ変数の宣言 Dim _CellBackColor As System.Drawing.Color Dim _CellForeColor As System.Drawing.Color 'コンストラクタ Public Sub New() '背景色の初期値を白にセット _CellBackColor = System.Drawing.Color.White '前景色の初期値を黒にセット _CellForeColor = System.Drawing.Color.Black End Sub '列の背景色を設定取得します Public Property CellBackColor() As Color Get Return _CellBackColor End Get Set(ByVal Value As Color) _CellBackColor = Value End Set End Property '列の前景色を設定取得します Public Property CellForeColor() As Color Get Return _CellForeColor End Get Set(ByVal Value As Color) _CellForeColor = Value End Set End Property 'Paintメソッドをオーバーライドします Protected Overloads Overrides Sub Paint( _ ByVal g As Graphics, _ ByVal bounds As Rectangle, _ ByVal source As CurrencyManager, _ ByVal rowNum As Integer, _ ByVal backBrush As Brush, _ ByVal foreBrush As Brush, _ ByVal alignToRight As Boolean _ ) 'セルの値を取得します Dim cellValue As Object = MyBase.GetColumnValueAtRow(source, rowNum) '前景色(文字)をセットします foreBrush = New SolidBrush(_CellForeColor) '背景色をセットします backBrush = New SolidBrush(_CellBackColor) '基本クラスのPaintメソッドを呼び出します MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight) End Sub End Class
' 使用方法 'クラスの使用方法ですが、 '①新しい列スタイルを作成する際にDataGridTextBoxColumnColorを用いて変数'を宣言します。 '②プロパティのCellBackColorでその列の背景色を、CellForeColorで前景色を'それぞれ設定します。 'それ以外のコードの説明はコメントを参照してください。 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DataSet1.ReadXml("C:\Work\Calendar.xml") 'データセットに格納されているテーブル0を 'DataTableオブジェクトに格納します Dim dt As DataTable = DataSet1.Tables(0) '新しいテーブル スタイルを宣言し、そのマップ名を設定します。 'マップ名にはテーブル名をセットします Dim DGTS As DataGridTableStyle = New DataGridTableStyle DGTS.MappingName = dt.TableName '新しい列スタイルを宣言します Dim style1 As DataGridTextColumnColor = New DataGridTextColumnColor Dim style2 As DataGridTextBoxColumn = New DataGridTextColumnColor Dim style3 As DataGridTextBoxColumn = New DataGridTextColumnColor Dim style4 As DataGridTextBoxColumn = New DataGridTextColumnColor Dim style5 As DataGridTextBoxColumn = New DataGridTextColumnColor Dim style6 As DataGridTextBoxColumn = New DataGridTextColumnColor Dim style7 As DataGridTextColumnColor = New DataGridTextColumnColor '列1の背景色を赤にセット style1.CellBackColor = System.Drawing.Color.Red '列7の背景色を青にセット style7.CellBackColor = System.Drawing.Color.Blue '列1の前景色を赤にセット style1.CellForeColor = System.Drawing.Color.White '列7の前景色を赤にセット style7.CellForeColor = System.Drawing.Color.White 'マップ名に列名を指定します style1.MappingName = dt.Columns(0).ColumnName style2.MappingName = dt.Columns(1).ColumnName style3.MappingName = dt.Columns(2).ColumnName style4.MappingName = dt.Columns(3).ColumnName style5.MappingName = dt.Columns(4).ColumnName style6.MappingName = dt.Columns(5).ColumnName style7.MappingName = dt.Columns(6).ColumnName 'ヘッダーテキストに列名をセットします style1.HeaderText = dt.Columns(0).ColumnName style2.HeaderText = dt.Columns(1).ColumnName style3.HeaderText = dt.Columns(2).ColumnName style4.HeaderText = dt.Columns(3).ColumnName style5.HeaderText = dt.Columns(4).ColumnName style6.HeaderText = dt.Columns(5).ColumnName style7.HeaderText = dt.Columns(6).ColumnName '作成した各列のスタイルDataGridTableStyleに追加します DGTS.GridColumnStyles.Add(style1) DGTS.GridColumnStyles.Add(style2) DGTS.GridColumnStyles.Add(style3) DGTS.GridColumnStyles.Add(style4) DGTS.GridColumnStyles.Add(style5) DGTS.GridColumnStyles.Add(style6) DGTS.GridColumnStyles.Add(style7) '作成したテーブルスタイルをDataGrid1に追加する dataGrid1.TableStyles.Add(DGTS) 'DataGridにデータを表示します DataGrid1.SetDataBinding(DataSet1, DataSet1.Tables(0).TableName) End Sub '使用したXMLファイル <?xml version="1.0" encoding="utf-8" ?> <CALENDAR> <WEEK> <SUN></SUN> <MON></MON> <TUE></TUE> <WED>1</WED> <THU>2</THU> <FRI>3</FRI> <SAT>4</SAT> </WEEK> <WEEK> <SUN>5</SUN> <MON>6</MON> <TUE>7</TUE> <WED>8</WED> <THU>9</THU> <FRI>10</FRI> <SAT>11</SAT> </WEEK> <WEEK> <SUN>12</SUN> <MON>13</MON> <TUE>14</TUE> <WED>15</WED> <THU>16</THU> <FRI>17</FRI> <SAT>18</SAT> </WEEK> <WEEK> <SUN>19</SUN> <MON>20</MON> <TUE>21</TUE> <WED>22</WED> <THU>23</THU> <FRI>24</FRI> <SAT>25</SAT> </WEEK> <WEEK> <SUN>26</SUN> <MON>27</MON> <TUE>28</TUE> <WED>29</WED> <THU>30</THU> <FRI></FRI> <SAT></SAT> </WEEK> </CALENDAR>