Wednesday, October 19

VB.NET: Listview control (Change the row backcolor of Filtered data)

This tricks is the continuation of my previous and first article VB.NET. Well this is very useful tricks, it will allow the user to change the backcolor of the row based on the condition.


SAMPLE:

 if the job title of the customers is owner then change the row color to blue.
 (not just the row color, we can also change the font style or color of the data)





In the example, I use radion button to do the filtering, but it depends of you how or what control you will use. We can use listbox, dropdown, combobox, textbox etc. After you choose the control you want, create an event on that control,in the example the event for radio box is CheckedChanged, and then copy the codes below:



(Populate the data first)

    For i = 0 To ListViewControl.Items.Count - 1
            ListViewControl.Items(i).BackColor = Color.White
            If ListViewControl.Items(i).SubItems(4).Text = "Purchasing Manager"  Then
                ListViewControl.Items(i).BackColor = Drawing.Color.Red
            End If
    Next i


I try to explain the codes;

1st:  first we use for loop to move to the next index,
2nd:    next we make the listview backcolor to anycolor we want, I use white for this.
3rd:      After that we do the checking;
                  Example:
             if the text of listview subitem(column index) is equal to "Owner"/"Puchasing Manager" then
4th:     change its backcolor to Red (or any color you want)
5th:   end the If statement
6th:  then move to the next i or index



Other example:

Job title is Purchasing Manager
  
   
Job title is Purchasing Representative

Like we know, not just the backcolor that we can change, we can also change font size, style and color.

Thank you reading, I hope you understand the logic.
 Hope you like it.





For I know the plans I have for you," declares the LORD, "plans to prosper you and not to harm you,
plans to give you hope and a future.

Jeremiah 29:11

To GOD all the GLORY




3 comments: