I need help...

I have a datagrid that show an online status. the Status column have 2 status, OnLine n OffLine. I want to make any record that Status = Online into bold style.

is that possible ??

all I know is to make all datagrid into bold..

please help.

Dani AI

Generated

— the goal is fine-grained styling for just the "Online" status. 's answer proves the idea, but doing style changes in the CellPainting event can lead to unnecessary redraw work and possible resource issues. A safer pattern is to set style in the CellFormatting event (or RowPrePaint when styling whole rows), compare the cell text case-insensitively, and reuse a single bold Font instance instead of allocating one per cell.

A compact, practical pattern (VB.NET WinForms) is shown below: create one bold Font at form load, use CellFormatting to check the Status column value, and assign that Font to e.CellStyle.Font when the value equals "Online". Dispose the Font when the form is disposed.

' form-level
Private boldFont As Font

' on form load
boldFont = New Font(DataGridView1.Font, FontStyle.Bold)

' CellFormatting event
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    If e.RowIndex < 0 Then Return
    If DataGridView1.Columns(e.ColumnIndex).Name <> "Status" Then Return
    Dim txt As String = If(e.Value, String.Empty).ToString().Trim()
    If String.Equals(txt, "Online", StringComparison.OrdinalIgnoreCase) Then
        e.CellStyle.Font = boldFont
    End If
End Sub

' dispose
Protected Overrides Sub Dispose(disposing As Boolean)
    If disposing Then boldFont?.Dispose()
    MyBase.Dispose(disposing)
End Sub

Notes: check for DBNull/Nothing before calling ToString, use the column Name rather than a hard-coded index so future reordering does not break styling, and remember selection highlighting can override appearance (adjust selection styles if needed). If the grid is actually an ASP.NET GridView (web forms) instead of WinForms, apply the same idea in the RowDataBound event. For the event details see the official docs: DataGridView.CellFormatting event.

Recommended Answers

All 2 Replies

Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        If e.RowIndex < 0 Or e.ColumnIndex < 1 Then
            Return
        End If

        If e.Value = "offline" Then
            Dim cellNew As New DataGridViewCellStyle
            cellNew.Font = New Font(Me.Font, FontStyle.Bold)
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style = cellNew
        End If
    End Sub

thanks... it work perfectly...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.