Results 1 to 4 of 4

Thread: alternate listview row color

  1. #1
    Hyperactive Member
    Join Date
    Sep 02
    Location
    Pilipinas
    Posts
    441

    alternate listview row color

    how to make the row of listview to have an alternate color??

  2. #2
    Hyperactive Member
    Join Date
    Jul 05
    Posts
    297

    Re: alternate listview row color

    set the color of the listviewitem, not the listview:

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim lv1 As New ListView
    3.         lv1.View = View.Details
    4.         lv1.Columns.Add("Column1", 120, HorizontalAlignment.Left)
    5.         lv1.Dock = DockStyle.Fill
    6.         For i As Integer = 0 To 15
    7.             Dim lvi As New ListViewItem("Item " & i + 1)
    8.             lvi.BackColor = Color.FromArgb(255, 255 - (i * 16), 255 - (i * 16), i * 16)
    9.             lvi.ForeColor = Color.FromArgb(255, i * 16, 255 - (i * 16), 255 - (i * 16))
    10.             lv1.Items.Add(lvi)
    11.         Next
    12.         Me.Controls.Add(lv1)
    13.     End Sub

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,242

    Re: alternate listview row color

    It would be a pain in the butt if your ListView is sortable, because you need to set the BackColor of each Item and then they will probably be out of order if sorted. You may be able to find an inherited ListView that already includes that functionality but I couldn't in a quick search. You could just create a method, either in a form or in an inherited ListView that alternately set the Item BackColors:
    VB Code:
    1. Private Sub SetAlternateItemColours()
    2.     For i As Integer = 0 To myListView.Items.Count - 1 Step 1
    3.         If i Mod 2 = 0 Then
    4.             myListView.Items(i).BackColor = Colors.Red
    5.         Else
    6.             myListView.Items(i).BackColor = Colors.Blue
    7.         End If
    8.     Next i
    9. End sub
    You would then make sure this method was called every time the data in the ListView changed, including when it was sorted. I haven't tested the performance of this code with a large number of Items, mind you.

  4. #4
    Member cops's Avatar
    Join Date
    Dec 07
    Posts
    44

    Re: alternate listview row color

    thanks john useful tread....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •