- for (int i = lv_panel_products.Items.Count - 1; i >= 0; i--)
- {
- ListViewItem lvi = lv_panel_products.Items[i];
- foreach (ListViewItem.ListViewSubItem subitem in lv_panel_products.Items)
- {
- if (lvi.Text.ToLower().Contains(txt_searchproducts.Text.ToLower()) || subitem.Text.ToLower().Contains(txt_searchproducts.Text.ToLower()))
- {
- lvi.BackColor = Color.Black;
- }
- else
- {
- lv_panel_products.Items.Remove(lvi);
- }
- }
- }
Multi Column Search In ListView ( C# ,Visual Studio 2015 )
HI , I am using this code on OnKeyPress event , which filters my list View, but it only searches in the 1st Column of the List View .. Need Help ..
Nitin SontakkePosted Aug 29, 2018, 1:55 AM
James AxsomPosted Aug 27, 2018, 1:34 PM
Arslan AliPosted Aug 27, 2018, 12:21 AM
James AxsomPosted Aug 26, 2018, 3:33 PM
Arslan AliPosted Aug 26, 2018, 1:09 AM
James AxsomPosted Aug 25, 2018, 8:48 PM
Arslan, my code and your code both iterate through items in a Listview control. It doesn’t matter how the list view is populated.
I’m going to break down your question word for word and interpret what you are requesting:
You said, “HI , I am using this code on OnKeyPress event , which filters my list View, but it only searches in the 1st Column of the List View .. Need Help “
If I had to rewrite your question, this would be my interpretation:
Key press event occurs and my listview filters on criteria. The criteria can be found on any row or in any column in the listview. The problem I’m encountering is the criteria only searches the 1st column in the listview control and seems to ignore the other columns. It seems like it ignores the other columns because I know I have matching criteria in other rows and columns in my listview not just the first column. If the criteria matches then the listview item changes its color to black otherwise it removes the listview item from the list.
Arslan, I offer you a listview with data. I show you the criteria of lemon. Lemon can found in two different rows and in two different columns. I highlight the specific listview items where lemon is found.
I did this because your main objective is “Multi Column Search In ListView”
The key line of code is setting UseItemsStyleForSubItems = false on the listview item. If you don’t to this then none of the other columns (or subitems) will change their color to black.
The problem is a subitem does not have a UserItemsStyleForSubItems property only a listview item. Your code iterates through only subitems, which is in a way is accurate because when you get down to it, listview items are nothing more than a collection of subitems.
However, if you want to change your color to black, then you have iterate through a listview item and its subitems in two loops.
Arslan AliPosted Aug 25, 2018, 11:55 AM
James AxsomPosted Aug 20, 2018, 1:21 PM