how to make the row of listview to have an alternate color??
how to make the row of listview to have an alternate color??
set the color of the listviewitem, not the listview:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim lv1 As New ListView lv1.View = View.Details lv1.Columns.Add("Column1", 120, HorizontalAlignment.Left) lv1.Dock = DockStyle.Fill For i As Integer = 0 To 15 Dim lvi As New ListViewItem("Item " & i + 1) lvi.BackColor = Color.FromArgb(255, 255 - (i * 16), 255 - (i * 16), i * 16) lvi.ForeColor = Color.FromArgb(255, i * 16, 255 - (i * 16), 255 - (i * 16)) lv1.Items.Add(lvi) Next Me.Controls.Add(lv1) End Sub
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: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.VB Code:
Private Sub SetAlternateItemColours() For i As Integer = 0 To myListView.Items.Count - 1 Step 1 If i Mod 2 = 0 Then myListView.Items(i).BackColor = Colors.Red Else myListView.Items(i).BackColor = Colors.Blue End If Next i End sub
2007-2013
Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB | C#
My Blog: Using Parameters in ADO.NET | Keyboard Events | Assemblies & Namespaces