Shakir Ali

Shakir Ali

  • NA
  • 359
  • 1.6k

How to search in datatable by datetime

Dec 14 2018 10:13 PM
Hello Everyone,
 
I have a DataTable that contains three columns ID, Shift Start and Shift End. I want to search all records in given date time suppose i want to search by "4/12/2018 22:53". I tried the following code but not working.
 
// Presuming the DataTable has a column named Date.
string expression;
expression = "[Shift Start] >= #4/12/2018 10:53:00 PM# AND [Shift End] <= #4/12/2018 10:53:00 PM#";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = dt.Select(expression);
// Print column 0 of each returned row.
for (int i = 0; i < foundRows.Length; i++)
{
MessageBox.Show(foundRows[i][1].ToString());
}
Sample DataTable
 
Thanks in advance.
 

Answers (1)