在 ListView C# 中搜索

发布于 2024-12-06 09:13:48 字数 2088 浏览 0 评论 0原文

我编写了一个方法来通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲娇萝莉攻 2024-12-13 09:13:48

我可能会将其存储为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文