如何使用searchstring搜索数据表中的行?

时间:2013-01-24 08:59:01

标签: c# listview datatable datarow

嗨我想在我的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();

        }

但我的字符串中有错误。如果我想搜索这些列,我该怎么办?

5 个答案:

答案 0 :(得分:34)

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

您可以使用DataTable.SelectDatView.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));

答案 1 :(得分:6)

如果其他人需要专门返回DataTable,您可以使用以下代码:

DataTable dtResult= tb.Select("CREATOR LIKE '%"+searchstring+"%'").CopyToDataTable();

答案 2 :(得分:1)

我刚为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 dtProcessesTemp = table.Clone();
        dtProcessesTemp.Clear();
        return dtProcessesTemp;
    }
    else
    {
        return filteredRows.CopyToDataTable();
    }
}

<强>用法:

DataTable dataTable = getData();
dataTable.SearchInAllColums(Keyword, StringComparison.OrdinalIgnoreCase);

答案 3 :(得分:0)

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

            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 ...

答案 4 :(得分: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();
    }
}