在数据表中的多列上应用过滤器

发布于 2025-01-27 04:24:05 字数 947 浏览 3 评论 0 原文

代码我正在使用

 (DataGridViewAllMusic.DataSource as DataTable).DefaultView.RowFilter = 
 string.Format("Title LIKE '%{0}%' or Artist LIKE '%{1}%' or Album LIKE '%{2}%' or 
 Comments LIKE '%{3}%' ",TextboxSearch.Text, TextboxSearch.Text, TextboxSearch.Text, 
 TextboxSearch.Text);

工作输出:

假设datagridview显示歌曲信息为(左侧的songtitle和右侧的艺术家)

title & 艺术家

温柔的生活& 三个度

和i Type life 在textboxsearch中datagridView将显示歌曲详细信息,因为word life 在datatable列中找到了 strong>

现在,如果我搜索,则在DataTable列中再次找到度 Artist ,因此DataGridView将显示相同的结果

所需的过滤器:

现在我还希望以艺术家和歌曲标题的方式搜索这首歌。

这样 三度温柔的寿命如果我在TextBoxSearch中搜索此信息,那么DataGridView也应显示与在单独搜索标题或艺术家时显示的结果相同的结果

Code i am using

 (DataGridViewAllMusic.DataSource as DataTable).DefaultView.RowFilter = 
 string.Format("Title LIKE '%{0}%' or Artist LIKE '%{1}%' or Album LIKE '%{2}%' or 
 Comments LIKE '%{3}%' ",TextboxSearch.Text, TextboxSearch.Text, TextboxSearch.Text, 
 TextboxSearch.Text);

Working output:

suppose datagridview showing song information as (SongTitle on left and Artist on right)

Title & Artist

A Tender Life & Three Degrees

and i type Life in TextboxSearch the datagridview will show the song detail since word Life is found in DataTable column Title

Now if i search Degree, again Degree is found in DataTable column Artist so the datagridview will show the same result

Desired filter:

Now i also want to search the song in a way like the artist and song title together.

such that
Three Degrees A Tender Life if i search this in TextboxSearch, the datagridview should also display the same result as displaying when searching the title or artist separately

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

世界和平 2025-02-03 04:24:05

特别感谢 @null博士。我找到了他的VB.NET代码,并在C#中使用。它效果很好!

vb.net博士

 var sb = new StringBuilder();
 var cols = new[] { "Title", "Artist" };
 var words = TextboxSearch.Text.Split(',').Where(x => x.Trim().Length > 0);

 for (var i = 0; i <= cols.Length - 1; i++)
 {
                               
    for (var j = 0; j <= words.Count() - 1; j++)
    {  
         sb.Append($"{cols[i].Trim()} LIKE ' 
         {words.ElementAt(j).Trim()}%'");
         if (j != words.Count() - 1)
            sb.Append(" OR ");
    }
     if (i != cols.Length - 1)
        sb.Append(" OR ");
 }
(DataGridViewAllMusicDark.DataSource as DataTable).DefaultView.RowFilter = 
sb.ToString();

A special thanks to @dr.null . I found his vb.net code and used in c#. It works great!

Vb.net by dr.null

 var sb = new StringBuilder();
 var cols = new[] { "Title", "Artist" };
 var words = TextboxSearch.Text.Split(',').Where(x => x.Trim().Length > 0);

 for (var i = 0; i <= cols.Length - 1; i++)
 {
                               
    for (var j = 0; j <= words.Count() - 1; j++)
    {  
         sb.Append(
quot;{cols[i].Trim()} LIKE ' 
         {words.ElementAt(j).Trim()}%'");
         if (j != words.Count() - 1)
            sb.Append(" OR ");
    }
     if (i != cols.Length - 1)
        sb.Append(" OR ");
 }
(DataGridViewAllMusicDark.DataSource as DataTable).DefaultView.RowFilter = 
sb.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文