我怎么可以在一个搜索一个DataTable搜索行

我怎么可以在一个搜索一个DataTable搜索行

本文介绍了我怎么可以在一个搜索一个DataTable搜索行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想搜索行,我的DataTable为了这个,我试试这个:

hi I want to search rows in my DataTable for this I try this:

 protected void imggastsuche_Click(object sender, EventArgs e)
        {
            string searchstring = txtgastsuche.Text;

            DataTable tb = DataBaseManager.GetDataTable(mysqlconnectionstring);

            DataRow[] foundRows = tb.Select("FIRSTNAME,LASTNAME,NAME,COMPANY,TIMEFROM,TIMETO,CREATOR Like '%" + searchstring + "%'");

            tb = foundRows.CopyToDataTable();

            this.ListView.DataSource = tb;
            this.ListView.DataBind();

        }

但我有我的字符串错误。我能做些什么,如果我想搜索此列?

But I have a error in my string . what can I do if I want search this columns?

推荐答案

您得到的错误,因为该参数为选择 filterEx pression 的并且已通过了所有列。了解filterEx pression为在SQL WHERE 条款。你想要的所有列,但你想只是一个筛选。你得到的所有列反正,因为它们是数据表 / 数据视图的一部分,所以你不必一一列举明确地。

You get the error because the parameter to Select is the filterExpression and you have passed all columns. Understand the filterExpression as a WHERE clause in sql. You want all columns but you want to filter by just one. You get all columns anyway since they are all part of the DataTable/DataView so you don't need to list them explicitely.

您既可以使用,<$c$c>DatView.RowFilter方法或

You could either use the DataTable.Select, DatView.RowFilter methods or LINQ-to-DataSet:

LINQ到数据集(我preFER):

LINQ-To-DataSet (which i prefer):

var filtered = tb.AsEnumerable()
    .Where(r => r.Field<String>("CREATOR").Contains(searchstring));

ADO.NET( DataTable.Select

DataRow[] filteredRows = tb.Select("CREATOR LIKE '%" + searchstring + "%'");

ADO.NET( DataView.RowFilter

 tb.DefaultView.RowFilter = "CREATOR LIKE '%" + searchstring + "%'";


如果你要搜索这个字符串在任何列而不是:

DataRow[] filteredRows = tb.Select("FIRSTNAME LIKE '%" + searchstring + "%' OR LASTNAME LIKE '%" + searchstring + "%' OR NAME LIKE '%" + searchstring + "%' OR COMPANY LIKE '%" + searchstring + "%' OR CREATOR LIKE '%" + searchstring + "%'");

使用LINQ是相同的:

The same with Linq:

var filtered = tb.AsEnumerable()
    .Where(r => r.Field<String>("FIRSTNAME").Contains(searchstring)
           ||   r.Field<String>("LASTNAME").Contains(searchstring))
           ||   r.Field<String>("NAME").Contains(searchstring)
           ||   r.Field<String>("COMPANY").Contains(searchstring)
           ||   r.Field<String>("CREATOR").Contains(searchstring));

这篇关于我怎么可以在一个搜索一个DataTable搜索行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 23:08