c# - Filtering items in a Listview -
i trying filter items in listview
using textbox
.
i've managed make something, can delete items listview, not bring them back. here little example of code:
private void textbox1_textchanged(object sender, eventargs e) { string value = textbox1.text.tolower(); (int = listview1.items.count - 1; -1 < i; i--) { if (listview1.items[i].text.tolower().startswith(value) == false) { listview1.items[i].remove(); } } }
does has idea on how retrieve deleted items? can't seem figure out >:...
check below sample app
using system; using system.windows.forms; using system.collections.generic; using system.linq; public partial class form1 : form { // keep list of listview items list<data> items = new list<data>(); public form1() { initializecomponent(); // initial data items = new list<data>(){ new data(){ id =1, name ="a"}, new data(){ id =2, name ="b"}, new data(){ id =3, name ="c"} }; // adding initial data listview1.items.addrange(items.select(c => new listviewitem(c.name)).toarray()); } private void textbox1_textchanged(object sender, eventargs e) { listview1.items.clear(); // clear list items before adding // filter items match search key , add result list view listview1.items.addrange(items.where(i=>string.isnullorempty(textbox1.text)||i.name.startswith(textbox1.text)) .select(c => new listviewitem(c.name)).toarray()); } } class data { public int id { get; set; } public string name { get; set; } }
Comments
Post a Comment