21

我想在我的DataTable.

我试过这个:

 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();

        }

但是我的字符串中有错误。

如果我想搜索这些列,我该怎么办?

4

5 回答 5

44

您收到错误是因为参数SelectfilterExpression并且您已经传递了所有列。将filterExpression理解为WHEREsql中的子句。您想要所有列,但只想按一列进行过滤。无论如何,您都会获得所有列,因为它们都是DataTable/的一部分,DataView因此您无需明确列出它们。

您可以使用DataTable.Select,DatView.RowFilter方法或LINQ-to-DataSet

LINQ-To-DataSet(我更喜欢):

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 + "%'";

string如果您想在任何列中搜索此内容:

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

与 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));
于 2013-01-24T09:03:32.477 回答
9

如果其他人需要专门返回一个 DataTable 你可以使用下面的代码:

DataTable dtResult= tb.Select("CREATOR LIKE '%"+searchstring+"%'").CopyToDataTable();
于 2016-09-07T22:35:56.483 回答
3

为此,我刚刚为 DataTable 类做了一个扩展方法。它返回一个只包含你想要的行的新数据表。

public static DataTable SearchInAllColums(this DataTable table, string keyword, StringComparison comparison)
{
    if(keyword.Equals(""))
    {
        return table;
    }
    DataRow[] filteredRows = table.Rows
           .Cast<DataRow>()
           .Where(r => r.ItemArray.Any(
           c => c.ToString().IndexOf(keyword, comparison) >= 0))
           .ToArray();

    if (filteredRows.Length == 0)
    {
        DataTable dtTemp = table.Clone();
        dtTemp.Clear();
        return dtTemp ;
    }
    else
    {
        return filteredRows.CopyToDataTable();
    }
}

用法:

DataTable dataTable = getData();
dataTable.SearchInAllColums(Keyword, StringComparison.OrdinalIgnoreCase);
于 2018-03-28T08:53:14.370 回答
0

您可以构建要在选择中使用的查询。

            if(TextBoxCusName.Text != "")
            {
                query = "CustomerName LIKE '%" + TextBoxCusName.Text.Trim()+"%' AND ";
            }
            if(TextBoxCusContact.Text != "")
            {
                query = query + "CustomerNo LIKE '%" + TextBoxCusContact.Text.Trim() + "%' AND ";
            }
            if(TextBoxVehicleNo.Text != "")
            {
                query = query + "VehicleNo LIKE '%" + TextBoxVehicleNo.Text.Trim()+"%'";
            }
            if(query.EndsWith("AND "))
            {
                query = query.Remove(query.Length - 4);
            }
            DataRow[] result = dataCustomerAndVehicle.Select(query);

这相当于

select * from dataCustomerAndVehicle where CustomerName LIKE '%...%' AND ...
于 2017-01-14T08:09:19.983 回答
0

搜索任何列的静态方法

public static DataTable SearchInAllColums(DataTable table, string keyword)
{
    StringComparison comparison = StringComparison.OrdinalIgnoreCase;

    if (keyword.Equals(""))
    {
        return table;
    }

    DataRow[] filteredRows = table.Rows
           .Cast<DataRow>()
           .Where(r => r.ItemArray.Any(
           c => c.ToString().IndexOf(keyword, comparison) >= 0))
           .ToArray();

    if (filteredRows.Length == 0)
    {
        DataTable dtTemp = table.Clone();
        dtTemp.Clear();
        return dtTemp;
    }
    else
    {
        return filteredRows.CopyToDataTable();
    }
}
于 2020-05-06T15:19:03.813 回答