Change font 1 of 3 ows in a datagrid

Napivo1972

Regular
Joined
Aug 31, 2004
Messages
85
Hello

I have a datagrid and I need to modify the font on 1 out of 3 Rows. Actually I just need to set it to bold. Like his:

Bold row
Normal row
Normal row
Bold row
Normal row
Normal row
Bold row
Normal row
Normal row
Etc.

Anyone have a way of doing this?
 
Code:
Anyone have a way of doing this?

Yes, I have, but unfortunately I don't have enough time to post a sample
code.

You'll have to subclass the DataGridTextBoxColumn class and trap its Paint
event Subs by overriding and overloading them. From inside Sub Paint, you'll
raise an event that will be trapped by your windows form.

Marcelo
 
I forgot to say that you'll have to pass the row number and a font object
from your custom DataGridTextBoxColumn to your form through that event,
modify the font object based on whether the row number is multiple of 3 and
then pass it back to the custom DataGridTextBoxColumn object, that will
paint the bolded/not bolded text in your DataGrid.

Marcelo
 
Ugh, this is not as easy as it looked. But I somehow managed. Ope I don’t et into trouble for this one as the code in the GetText and PaintTxt are only slight adaptations from the original DataGridTextBoxColumn code. I’d like comments on this one.

Visual Basic:
Imports System.ComponentModel

Public Class myDataGridTextBoxColumn

    Inherits System.Windows.Forms.DataGridTextBoxColumn

    Private typeConverter As New typeConverter
    Private Const yMargin = 1

    Protected Overrides Sub Abort(ByVal rowNum As Integer)
        MyBase.Abort(rowNum)
    End Sub

    Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean
        Return MyBase.Commit(dataSource, rowNum)
    End Function

    Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, _
                                           ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, _
                                           ByVal instantText As String, ByVal cellIsVisible As Boolean)

        MyBase.Edit(source, rowNum, bounds, [readOnly], instantText, cellIsVisible)
    End Sub

    Protected Overrides Function GetMinimumHeight() As Integer
        Return MyBase.GetMinimumHeight
    End Function

    Protected Overrides Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As Object) As Integer
        Return MyBase.GetPreferredHeight(g, value)
    End Function

    Protected Overrides Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size
        Return MyBase.GetPreferredSize(g, value)
    End Function

    Private Function GetText(ByVal value As Object) As String
        If TypeOf value Is DBNull Then
            Return Me.NullText
        End If
        If (((Not Me.Format Is Nothing) AndAlso (Me.Format.Length <> 0)) AndAlso TypeOf value Is IFormattable) Then
            Try
                Return CType(value, IFormattable).ToString(Me.Format, Me.FormatInfo)
            Catch exception1 As Exception
                GoTo Label_0084
            End Try
        End If
        If ((Not Me.typeConverter Is Nothing) AndAlso Me.typeConverter.CanConvertTo(GetType(String))) Then
            Return CType(Me.typeConverter.ConvertTo(value, GetType(String)), String)
        End If
Label_0084:
        If (value Is Nothing) Then
            Return ""
        End If
        Return value.ToString
    End Function

    Protected Sub PaintTxt(ByVal g As Graphics, ByVal Bounds As Rectangle, ByVal [text] As String, _
                           ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean _
                           , ByVal rowNum As Long)
        Me.DataGridTableStyle.BackColor = System.Drawing.Color.Beige
        Dim rect As RectangleF
        rect.X = Bounds.X
        rect.Y = Bounds.Y
        rect.Height = Bounds.Height
        rect.Width = Bounds.Width
        Dim format1 As New StringFormat
        If alignToRight Then
            format1.FormatFlags = (format1.FormatFlags Or StringFormatFlags.DirectionRightToLeft)
        End If

        g.FillRectangle(backBrush, rect)
        rect.Offset(0, (2 * Me.yMargin))
        rect.Height = (rect.Height - (2 * Me.yMargin))

        If rowNum Mod 3 <> 0 Then
            g.DrawString(text, Me.DataGridTableStyle.DataGrid.Font, foreBrush, rect, format1)
        Else
            Dim fnt As New Font(Me.DataGridTableStyle.DataGrid.Font, FontStyle.Bold)
            g.DrawString(text, fnt, foreBrush, rect, format1)
        End If
    End Sub

    Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, _
                                            ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, _
                                            ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, _
                                            ByVal alignToRight As Boolean)
        Dim text1 As String = Me.GetText(Me.GetColumnValueAtRow(source, rowNum))
        Me.PaintTxt(g, bounds, text1, alignToRight, rowNum)
    End Sub

    Protected Sub PaintTxt(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [text] As String, _
                           ByVal alignToRight As Boolean, ByVal Rownum As Long)
        Dim forebrush As System.Drawing.Brush = System.Drawing.Brushes.Black
        Dim Backbrush As System.Drawing.Brush = System.Drawing.Brushes.White
        Me.PaintTxt(g, bounds, text, Backbrush, forebrush, alignToRight, Rownum)
    End Sub

    Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, _
                                            ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer)

        Me.Paint(g, bounds, source, rowNum, False)
    End Sub
End Class
 
Back
Top