22

I want to search rows in my DataTable.

I've tried 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 an error in my string.

What can I do if I want to search these columns?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Tarasov
  • 3,625
  • 19
  • 68
  • 128

5 Answers5

46

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.

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

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

If you want to search for this string in any column instead:

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

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));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
9

If some one else needs return specifically a DataTable you can use the code below:

DataTable dtResult= tb.Select("CREATOR LIKE '%"+searchstring+"%'").CopyToDataTable();
ejhn5
  • 105
  • 3
  • 8
4

I just made a extension method to the DataTable class for this. It returns a new Datatable containing only the rows you want.

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

Usage:

DataTable dataTable = getData();
dataTable.SearchInAllColums(Keyword, StringComparison.OrdinalIgnoreCase);
Tom Stein
  • 345
  • 2
  • 15
0

you could build the query you are going to use in the 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);

this equivalent to

select * from dataCustomerAndVehicle where CustomerName LIKE '%...%' AND ...
Naseeruddin V N
  • 597
  • 5
  • 17
0

Static method for search any column

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();
    }
}
Joe Mayo
  • 7,501
  • 7
  • 41
  • 60