I want to check if a data table entry contains part of a user input string
Eg In the data table if there is 123456, if the user enters 1234, I should get a match and get the values in that row.
Right now I can get the values in the row if there is an exact match,
My code for checking is:
string findPO = string.Format("PO = {0}",tbPOnum.Text);
DataRow[] selectRow = localDataTable.Select(findPO);
localDataTable.Rows.IndexOf(selectRow[0]);
Thanks for any help
It should work with the LIKE expression
string findPO = string.Format("Convert (PO,System.String) LIKE '*{0}*'",tbPOnum.Text);
DataRow[] selectRow = localDataTable.Select(findPO);
This should return all rows containing the value.
For more information about the expressions you can use with the Select method, see information on DataColumn.Expression
Both the * and % can be used interchangeably for wildcard characters in a LIKE comparison. If the string in a LIKE clause contains a * or %, those characters should be enclosed in brackets ([]). If a bracket is in the clause, each bracket character should be enclosed in brackets (for example [[] or []]). A wildcard is allowed at the start and end of a pattern, or at the end of a pattern, or at the start of a pattern.
Wildcard characters are not allowed in the middle of a string. For example, 'te*xt' is not allowed.
@Jehof - The answer you provided was right (using the LIKE filter statement)
The field I was trying to search for was an int field, After some research I got the following code which worked:
string findPO = string.Format("convert(PO, 'System.String') LIKE '%{0}%'", tbPOnum.Text);
Just loop rows and then columns (other vice versa) and check for any values of cells, for which .IndexOf(findPO) == 0
. That means that the value of the cell begins with the given string. You may use linq as well. If you need a code example, just share.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.