質問が長すぎる場合は申し訳ありませんが、質問する前に説明する必要があります。
設定した条件を満たしたセルの文字色を変更したい。
また、水平線を 2 行ごとに、垂直線を 5 列ごとに描画したいと考えています。
私はそれらの両方を別々に行うことができます。問題は、両方を同時に取得できないことです。
最初にテキストを変更すると、影響を受けるセルの境界線が消えてしまいます。
最初に境界線を描画すると、テキストの色が黒 (デフォルト) に戻ります。
セルのテキストの色と境界線の色を変更する必要があります。
私のコードは次のとおりです。
Private Sub GridBorderLine(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
'Declare Border Style and Border Color
Dim vBStyle As ButtonBorderStyle = ButtonBorderStyle.Solid
Dim vBColor As Color = Color.Black
'Custom Row Border (Horizontal)
If e.RowIndex Mod 2 = False AndAlso e.RowIndex > 0 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All - DataGridViewPaintParts.ContentBackground)
Dim H As Integer = 0
ControlPaint.DrawBorder(e.Graphics, e.CellBounds, vBColor, 0, vBStyle, vBColor, 3, vBStyle, vBColor, 0, vBStyle, vBColor, 0, vBStyle)
e.Handled = True
End If
'Custom Column Border (Vertical)
If e.ColumnIndex Mod 5 = False AndAlso e.ColumnIndex > 0 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All - DataGridViewPaintParts.ContentBackground)
Dim W As Integer = 0
If e.RowIndex Mod 2 = False And e.RowIndex > 0 Then W = 3 Else W = 0
ControlPaint.DrawBorder(e.Graphics, e.CellBounds, vBColor, 3, vBStyle, vBColor, W, vBStyle, vBColor, 0, vBStyle, vBColor, 0, vBStyle)
e.Handled = True
End If
'Custom Cell Color
If e.FormattedValue.ToString.Contains(txt.Text) Then
CellColor(e)
End If
End Sub
Private Sub CellColor(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
Dim vLength as Integer = txt.Text.Length
e.Paint(e.CellBounds, DataGridViewPaintParts.All - DataGridViewPaintParts.ContentForeground)
If e.FormattedValue.ToString = "" Then Exit Sub
Dim vBeginning As String = e.FormattedValue.ToString.Substring(0, vLength)
Dim vEnd As String = e.FormattedValue.ToString.Substring(vLength)
Dim s As Size = TextRenderer.MeasureText(e.FormattedValue.ToString, vFont)
Dim f As Size = TextRenderer.MeasureText(vBeginning, vFont)
Dim b As Size = TextRenderer.MeasureText(vEnd, vFont)
'Arrange Center Middle
Dim w = e.CellBounds.X + ((e.CellBounds.Width - s.Width) / 2)
Dim h = e.CellBounds.Y + ((e.CellBounds.Height - s.Height) / 2)
Dim topLeft As New Point(w, h)
Dim p As New Point(topLeft.X + (s.Width - b.Width), topLeft.Y)
TextRenderer.DrawText(e.Graphics, vBeginning, vFont, topLeft, Color.Red)
TextRenderer.DrawText(e.Graphics, vEnd, vFont, p, Color.Blue)
e.Handled = True
End Sub