I've wrote a method to search thru ListView for a given string and mark the ones that are found with Color
. It works fine however with lots of information on screen and scrollable ListView it's sometimes hard to find what user is looking for.
Normally I do create special
searches by modifying method and SQL query WHERE
clause but it's always a pain and requires more work/code for each List开发者_运维问答View/Data.
I would like to have some generalized search that would work for all kind of searches in ListView just like one I have now but with ability to hide (rows) what's not needed and show only necessary rows. Of course if one changes it's mind it has to bring back the old rows back.
I guess the biggest problem for me is how to store all the columns and data without over complicating knowing that it can be 3 to 20+ columns and multiple rows.
public static void wyszukajNazweListView(ListView varListView, string varWyszukaj) {
if (varWyszukaj != "") {
foreach (ListViewItem comp in varListView.Items) {
comp.UseItemStyleForSubItems = false;
foreach (ListViewItem.ListViewSubItem drv in comp.SubItems) {
string textToAdd2 = drv.Text;
if (textToAdd2.Length >= 1) {
if (textToAdd2.ToLower().Contains(varWyszukaj.ToLower())) {
drv.BackColor = Color.DarkOrange;
} else {
drv.BackColor = Color.White;
}
}
}
bool varColor = false;
foreach (ListViewItem.ListViewSubItem drv in comp.SubItems) {
if (drv.BackColor == Color.DarkOrange) {
varColor = true;
break;
}
}
if (varListView.SmallImageList != null) {
if (varColor) {
comp.ImageIndex = 2;
} else {
comp.ImageIndex = -1;
}
}
}
} else {
foreach (ListViewItem comp in varListView.Items) {
comp.UseItemStyleForSubItems = false;
comp.BackColor = Color.White;
foreach (ListViewItem.ListViewSubItem drv in comp.SubItems) {
drv.BackColor = Color.White;
comp.ImageIndex = -1;
}
}
}
}
I'd probably store it as a DataTable
object. DataTable
type allows setting its rows as hidden (e.g. Visible = false
) and you can bind your ListView directly to it.
EDIT: noticed the WinForms
tag. Even simpler: no need to mock about with ViewState/Session.
精彩评论