在 ListView C# 中搜索
我编写了一个方法来通过 ListView 搜索给定的字符串,并用 Color
标记找到的字符串。它工作得很好,但是屏幕上有大量信息和可滚动的 ListView,有时很难找到用户正在寻找的内容。
通常,我会通过修改方法和 SQL 查询 WHERE
子句来创建特殊
搜索,但这总是很痛苦,并且需要为每个 ListView/Data 进行更多工作/代码。
我想要一些通用搜索,它适用于 ListView 中的所有类型的搜索,就像我现在拥有的搜索一样,但能够隐藏(行)不需要的内容并仅显示必要的行。当然,如果有人改变主意,就必须恢复旧的行。
我想对我来说最大的问题是如何存储所有列和数据,而不需要过于复杂地知道它可以是 3 到 20 多个列和多行。
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'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 ListView/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;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能会将其存储为 DataTable 对象。
DataTable
类型允许将其行设置为隐藏(例如Visible = false
),并且您可以将 ListView 直接绑定到它。编辑:注意到
WinForms
标签。甚至更简单:无需使用 ViewState/Session 进行模拟。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.