I want to color cell of the listview, how to write code ? You see my image file.

Dani AI

Generated

Short summary and practical options (linked references at the end): the WinForms ListView does not expose a simple “per-cell color” toggle in the designer, but single-cell styling is possible two ways: set an item’s UseItemStyleForSubItems = False and give individual SubItems a BackColor/ForeColor for simple needs, or enable OwnerDraw and paint each subitem for full control. For grid-like UIs where many cells need independent styling, the DataGridView is the easier, built‑in choice. was correct that the default behavior favors row coloring; pointed toward the built-in API that makes subitem colors possible. (learn.microsoft.com) (UseItemStyleForSubItems, SubItem.BackColor, OwnerDraw / DrawSubItem).

OwnerDraw example (VB.NET) — highlights a single subitem background based on content (adapt for an attendance timestamp by parsing the date and applying a color rule):

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ListView1.View = View.Details
    ListView1.OwnerDraw = True
    ' add columns/items...
End Sub

Private Sub ListView1_DrawItem(sender As Object, e As DrawListViewItemEventArgs) Handles ListView1.DrawItem
    ' For Details view, leave item background to DrawSubItem
    If ListView1.View <> View.Details Then e.DrawDefault = True
End Sub

Private Sub ListView1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
    Dim bgcolor As Color = Color.White
    Dim colIndex = e.ColumnIndex

    ' Example rule: column 2 contains a numeric status or timestamp
    Dim val As Integer
    If colIndex = 2 AndAlso Integer.TryParse(e.SubItem.Text, val) AndAlso val < 0 Then
        bgcolor = Color.LightSalmon
    End If

    Using br As New SolidBrush(bgcolor)
        e.Graphics.FillRectangle(br, e.Bounds)
    End Using

    TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font, e.Bounds, e.SubItem.ForeColor, TextFormatFlags.Left)
    If (e.ItemState And ListViewItemStates.Selected) <> 0 Then e.DrawFocusRectangle()
End Sub

Notes and troubleshooting: set each ListViewItem.UseItemStyleForSubItems = False when relying on SubItem.BackColor/ForeColor; OwnerDraw gives complete control but requires handling selection/focus and the DrawItem/DrawSubItem/DrawColumnHeader events carefully (there are known quirks around hover/extra DrawItem calls). For heavy per-cell rules (for example coloring timestamps older than a threshold across many rows), using DataGridView avoids owner-draw complexity.

Recommended Answers

All 5 Replies

As far as I know you can't change the colour of an individual cell in a listview (details view). You can only change the colour of an entire row.

I do not know how to write code. I'm looking for an example of highlighting cell the listView of winform or an example of an employee timestamp based on the listView cell color, you can see the attached image file above.

This may be silly question, but if you don't know how to write code then how are you planning to implement it?

Now that's a horse of a different color. You changed your question from How? to "I need to hire a programmer or outsource to accomplish this."

That's OK, now post in the For Hire/Hiring forum with the job details, pay and such.

You may also want to omit the c tag since I didn't find the needed API call in c. The needed API is callable from C#, C++, F# and VB. The example code at the link I provided is in C#, C++ and VB but not F# so no big deal since you didn't list F#.

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.