Я пытаюсь отфильтровать DateTime в DataTable и испытываю странную проблему с фильтрацией.
1) Данные и фильтрация - работает нормально
public DataTable GetDataTable()
{
DataTable employeeCollection = new DataTable();
var dt = DateTime.Now;
employeeCollection.Columns.Add("EmployeeID", typeof(int));
employeeCollection.Columns[0].ColumnName = "Employee ID";
employeeCollection.Columns.Add("EmployeeName", typeof(string));
employeeCollection.Columns["EmployeeName"].ColumnName = "Employee Name";
employeeCollection.Columns.Add("CustomerID", typeof(string));
employeeCollection.Columns["CustomerID"].ColumnName = "Customer ID";
employeeCollection.Columns.Add("Country", typeof(string));
employeeCollection.Columns.Add("Date", typeof(DateTime));
DateTime date1 = new DateTime(2011, 6, 26, 4, 34, 45);
DateTime date2 = new DateTime(2011, 6, 27, 4, 34, 45);
employeeCollection.Rows.Add(1011, "DintinAmam", "Alfki", "Britain", date1);
employeeCollection.Rows.Add(1012, "JohnAmam", "Johanesberg", "China", date2);
string filterString = "Date = #" + date1.ToString() + "#";
employeeCollection.DefaultView.RowFilter = filterString;
return employeeCollection;
}
Строка фильтра - «Дата = # 6/26/2011 4:34:45 AM #»
Output1:
![enter image description here](https://i.stack.imgur.com/8rLyF.png)
2) Данные и фильтрация - не работает
public DataTable GetDataTable()
{
DataTable employeeCollection = new DataTable();
var dt = DateTime.Now;
employeeCollection.Columns.Add("EmployeeID", typeof(int));
employeeCollection.Columns[0].ColumnName = "Employee ID";
employeeCollection.Columns.Add("EmployeeName", typeof(string));
employeeCollection.Columns["EmployeeName"].ColumnName = "Employee Name";
employeeCollection.Columns.Add("CustomerID", typeof(string));
employeeCollection.Columns["CustomerID"].ColumnName = "Customer ID";
employeeCollection.Columns.Add("Country", typeof(string));
employeeCollection.Columns.Add("Date", typeof(DateTime));
DateTime date1 = DateTime.Now.AddDays(1);
DateTime date2 = DateTime.Now.AddDays(2);
employeeCollection.Rows.Add(1011, "DintinAmam", "Alfki", "Britain", date1);
employeeCollection.Rows.Add(1012, "JohnAmam", "Johanesberg", "China", date2);
string filterString = "Date = #" + date1.ToString() + "#";
employeeCollection.DefaultView.RowFilter = filterString;
return employeeCollection;
}
Строка фильтра - «Дата = # 6/27/2018 5:19:17 PM #»
Выход2:
![enter image description here](https://i.stack.imgur.com/JUjgT.png)
При создании значения DateTime с помощью конструктора фильтра работает нормально, а при использовании DateTime.Now фильтрация не работает.
Может ли кто-нибудь подтвердить мне, почему инициализация значения Date с использованием DateTime.Now.AddDays () не работает для фильтрации?