- 題名: DataGridの特定セルの背景色変更
- 日時: 2003/10/17 15:40:22
- ID: 973
- この記事の返信元:
- (なし)
- この記事への返信:
- [975] Re[1]: DataGridの特定セルの背景色変更2003/10/17 19:14:02
- [1114] Re[1]: DataGridの特定セルの背景色変更(長文)2003/10/23 16:51:22 [解決]
- ツリーを表示
■No973に返信(どらごらさんの記事)
> お久しぶりです。また質問があります。
>
> 特定のセルだけ背景色を変更したいのですが、どぼんさんのTipsにある
> DataGridColumnStyleからの派生クラスしかないのでしょうか?
>
> DataGrid.CurrentCell.bkColor = Color.Red
> or
> DataGrid.CurrentCell.bkColor = Brushes.Red
>
> 上記の様な感じでできると、すごく使い易いのですが。。。
> なにか方法がありますでしょうか? よろしくお願いします。
>
>
DataGridTextBoxColumnの派生でできますよ。
下記のクラスをカスタマイズして「GridColumnStyles」に追加してください。
Public Class DataGridTextBoxColumnSetColor
Inherits DataGridTextBoxColumn
Public Sub New()
End Sub
'自分の大嫌いな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 objValue As Object
Dim intValue As Integer
Try
'例では、セルに0か1しか入っていないことを想定
'変数の持ち方を変えれば応用は利きますよ!
objValue = MyClass.GetColumnValueAtRow(source, rowNum)
If IsNothing(objValue) = False Then
If IsDBNull(objValue) = False Then
If IsNumeric(objValue) = True Then
intValue = CType(objValue, Integer)
If intValue = 0 Then
'背景を赤、前景を青
backBrush = New SolidBrush(Color.Red)
foreBrush = New SolidBrush(Color.Blue)
ElseIf intValue = 1 Then
'背景を黄、前景を赤
backBrush = New SolidBrush(Color.Yellow)
foreBrush = New SolidBrush(Color.Red)
End If
End If
End If
End If
Catch ex As Exception
'エラー時は、デフォルトの色
Finally
'描画
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
End Try
End Sub
End Class
■No975に返信(fukuさんの記事) レスありがとうございます。 今日は用事があるため、月曜に検証してみます。すみません。 ただ... > Try > Catch ex As Exception > Finally > End Try これができるのには、感動しました^^; できないと(勝手に)思い込んでいたので、ネスト階層が減りますね。
どらごらです。
難しく考えないで、MyDataGridTextBoxColumnにイベントを1つ追加しました。
そしたら、まぁ〜100%期待通りと言うわけでは、無いですがうまくいきました。
#PaintイベントをPublicにすればいい?
追加したCellPaintイベントで、好きなように(アプリ側で)処理できるので
一応満足しました。ただ他にもありそうな気がするんですけどね^^;
皆さん、ありがとうございました。
----
Public Class Form3
Inherits System.Windows.Forms.Form
#Region " Windows フォーム デザイナで生成されたコード "
'Form3にDataGrid1を置いただけです。
...
#End Region
Private Sub Form3_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load
Dim tmpTable As System.Data.DataTable = New DataTable("DataTable1")
Dim tmpRow As System.Data.DataRow
Dim ts As New DataGridTableStyle
Dim cs As MyDataGridTextBoxColumn
'初期化
Randomize()
tmpTable.Columns.Add("Column1")
tmpTable.Columns.Add("Column2")
'データ追加(当方2003のため)
For I As Integer = 0 To 5
tmpRow = tmpTable.NewRow()
tmpRow(0) = CStr(Int(2 * Rnd())) '0-1 as String)
tmpRow(1) = CStr(Int(2 * Rnd())) '0-1 as String
tmpTable.Rows.Add(tmpRow)
Next
DataGrid1.DataSource = tmpTable
'Tipsそのまんま
ts.MappingName = "DataTable1"
cs = New MyDataGridTextBoxColumn
cs.MappingName = "Column1"
AddHandler cs.CellPaint, AddressOf gcsPaint '追加
ts.GridColumnStyles.Add(cs)
cs = New MyDataGridTextBoxColumn
cs.MappingName = "Column2"
AddHandler cs.CellPaint, AddressOf gcsPaint '追加
ts.GridColumnStyles.Add(cs)
DataGrid1.TableStyles.Add(ts)
End Sub
Private Sub gcsPaint( _
ByVal ColumnMappingName As String, _
ByVal rowNum As Integer, _
ByRef backBrush As Brush, _
ByRef foreBrush As Brush _
)
backBrush = Brushes.White
Select Case ColumnMappingName
Case "Column1"
If rowNum Mod 2 = 0 Then
backBrush = Brushes.Red
End If
Case "Column2"
If rowNum Mod 2 = 0 Then
backBrush = Brushes.Yellow
End If
End Select
End Sub
End Class
Public Class MyDataGridTextBoxColumn
Inherits DataGridTextBoxColumn
Public Event CellPaint( _
ByVal ColumnMappingName As String, _
ByVal rowNum As Integer, _
ByRef backBrush As Brush, _
ByRef foreBrush As Brush _
)
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 _
)
RaiseEvent CellPaint(Me.MappingName, rowNum, backBrush, foreBrush)
MyBase.Paint(g, bounds, source, rowNum, _
backBrush, foreBrush, alignToRight)
End Sub
End Class
お久しぶりです。また質問があります。 特定のセルだけ背景色を変更したいのですが、どぼんさんのTipsにある DataGridColumnStyleからの派生クラスしかないのでしょうか? DataGrid.CurrentCell.bkColor = Color.Red or DataGrid.CurrentCell.bkColor = Brushes.Red 上記の様な感じでできると、すごく使い易いのですが。。。 なにか方法がありますでしょうか? よろしくお願いします。