DataGridViewの色に関して

解決


misamisa  2012-07-18 09:30:32  No: 103321  IP: 192.*.*.*

開発環境:VIsual Basic 2008

あるデータをDataGridViewに表示させていますが、その際に、同じ区分のデータで
あれば、グループ化(色を変える)したいと考え、以下の記述をあるHPから検索し、プログラムしました。

やりたい事はこれに近かった為、これを加工しましたが、グループ化した際の色が
"緑"となってしまっている為、この色をなんとか変更できないか?と考えています。

どこを変更すれば良いか教えて下さい。

よろしくお願いします。

《記述内容》
'デフォルトのセルスタイル
Private defaultCellStyle As DataGridViewCellStyle
'グループ化された一番上の行のセルスタイル
Private groupCellStyle As DataGridViewCellStyle

'フォームのLoadイベントハンドラ
Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
    'セルスタイルを設定する
    Me.defaultCellStyle = New DataGridViewCellStyle()
    Me.groupCellStyle = New DataGridViewCellStyle()
    Me.groupCellStyle.ForeColor = Color.White
    Me.groupCellStyle.BackColor = Color.DarkGreen
    Me.groupCellStyle.SelectionBackColor = Color.DarkBlue
End Sub

'CellFormattingイベントハンドラ
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, _
        ByVal e As DataGridViewCellFormattingEventArgs) _
        Handles DataGridView1.CellFormatting
    Dim dgv As DataGridView = CType(sender, DataGridView)
    'セルが1列目で、ヘッダーではなく、新しい行でもないとき
    If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 AndAlso _
        e.RowIndex <> dgv.NewRowIndex Then
        If e.RowIndex = 0 OrElse _
            Not dgv(e.ColumnIndex, e.RowIndex - 1).Value.Equals(e.Value) Then
            '1行目か、上のセルと違う値の時は、背景色を変更する
            dgv.Rows(e.RowIndex).DefaultCellStyle = Me.groupCellStyle
        Else
            dgv.Rows(e.RowIndex).DefaultCellStyle = Me.defaultCellStyle
            e.Value = ""
            e.FormattingApplied = True
        End If
    End If
End Sub

編集 削除
魔界の仮面弁士  2012-07-18 11:15:56  No: 103322  IP: 192.*.*.*

> "緑"となってしまっている為、この色をなんとか変更できないか?と考えています。

提示されたコードで、グループ色を指定している個所は下記ですね。
  Me.groupCellStyle.ForeColor = Color.White
  Me.groupCellStyle.BackColor = Color.DarkGreen
  Me.groupCellStyle.SelectionBackColor = Color.DarkBlue

' ForeColor(文字色) は White(白)
' BackColor(背景色) は DarkGreen(暗緑)
' SelectionForeColor(選択文字色) は Empty(継承色)
' SelectionBackColor(選択背景色) は DarkBlue(暗青)


> どこを変更すれば良いか教えて下さい。
Color 系プロパティの設定を変更すれば良いかと。

編集 削除
misamisa  2012-07-18 11:36:18  No: 103323  IP: 192.*.*.*

魔界の仮面弁士様、いつもご回答ありがとうございます。

質問しておきながらご指摘頂いた内容ではっと気が付きました。

Load処理の中で色指定していました、、、

ありがとうございました!!!

編集 削除