c#bindingsource筛选器日期时间早于x天

c#bindingsource筛选器日期时间早于x天,c#,sql,datagridview,filter,bindingsource,C#,Sql,Datagridview,Filter,Bindingsource,我正在寻找一种解决方案,通过一个早于x天的datetime字段1过滤datagridview,其中x是同一行中键入short的另一个字段2。在sql中,可能的语法为: field1 < DATEADD(day, field2, GETDATE()) field1DateTime.Now.Subtract(a)>TimeSpan.FromDays(1)); //一天前 如果需要更准确的帮助,请提供更多信息和代码片段 编辑1: 如果您正在使用带有datetime属性的自定义类型: List

我正在寻找一种解决方案,通过一个早于x天的datetime字段1过滤datagridview,其中x是同一行中键入short的另一个字段2。在sql中,可能的语法为:

field1 < DATEADD(day, field2, GETDATE())
field1
但是我必须使用
DataGridView绑定到BindingSource,而源绑定到sql数据库中的已填充数据。

绑定到DataGridView的是什么?怎么做

如果您在codebehind中绑定到它,并且您的数据源是一个日期时间列表:

List<DateTime> dtList; //list with datetimes
dtList.Where( a => DateTime.Now.Subtract(a) > TimeSpan.FromDays(1)); 
//More than 1 day ago
列表dtList//带日期时间的列表
其中(a=>DateTime.Now.Subtract(a)>TimeSpan.FromDays(1));
//一天前
如果需要更准确的帮助,请提供更多信息和代码片段

编辑1:

如果您正在使用带有datetime属性的自定义类型:

List<CustomObject> objList; //list with Custom objects
objList.Where( a => DateTime.Now.Subtract(a.dateTimePropertyName) > TimeSpan.FromDays(1))
.Where(a => a.otherPropertyName == "ExampleValue"); 

DateGridViewName.DataContext = objList;
列表对象列表//带有自定义对象的列表
其中(a=>DateTime.Now.Subtract(a.dateTimePropertyName)>TimeSpan.FromDays(1))
。其中(a=>a.otherPropertyName==“ExampleValue”);
DateGridViewName.DataContext=objList;

您绑定到DataGridView的是什么?怎么做

如果您在codebehind中绑定到它,并且您的数据源是一个日期时间列表:

List<DateTime> dtList; //list with datetimes
dtList.Where( a => DateTime.Now.Subtract(a) > TimeSpan.FromDays(1)); 
//More than 1 day ago
列表dtList//带日期时间的列表
其中(a=>DateTime.Now.Subtract(a)>TimeSpan.FromDays(1));
//一天前
如果需要更准确的帮助,请提供更多信息和代码片段

编辑1:

如果您正在使用带有datetime属性的自定义类型:

List<CustomObject> objList; //list with Custom objects
objList.Where( a => DateTime.Now.Subtract(a.dateTimePropertyName) > TimeSpan.FromDays(1))
.Where(a => a.otherPropertyName == "ExampleValue"); 

DateGridViewName.DataContext = objList;
列表对象列表//带有自定义对象的列表
其中(a=>DateTime.Now.Subtract(a.dateTimePropertyName)>TimeSpan.FromDays(1))
。其中(a=>a.otherPropertyName==“ExampleValue”);
DateGridViewName.DataContext=objList;

这将对静态数据起作用

DataTable filtered = dt.AsEnumerable().Where(
    x => x.Field<DateTime>("field1") < DateTime.Today.AddDays(x.Field<int>("field2"))).
    CopyToDataTable();

bindingSource1.DataSource = filtered;
DataTable filtered=dt.AsEnumerable()。其中(
x=>x.Field(“field1”)
然而,如果我理解您想要什么(数据绑定),尽管可能有一种方法可以在DataTable中实现,但它听起来像是域对象的作业

class BoundDataObject
{
    public DateTime Field1 { get; set; }
    public short Field2 { get; set; }
    public bool PassFail
    {
        get { return Field1 < DateTime.Now.AddDays(Field2); }
    }
}
类BoundDataObject
{
公共日期时间字段1{get;set;}
公共短字段2{get;set;}
公共布尔PassFail
{
获取{returnfield1

将数据加载到域对象可能比数据表稍微费力一些,但这是一次性的成本,我认为您将很快获得好处。

这将对静态数据有效

DataTable filtered = dt.AsEnumerable().Where(
    x => x.Field<DateTime>("field1") < DateTime.Today.AddDays(x.Field<int>("field2"))).
    CopyToDataTable();

bindingSource1.DataSource = filtered;
DataTable filtered=dt.AsEnumerable()。其中(
x=>x.Field(“field1”)
然而,如果我理解您想要什么(数据绑定),尽管可能有一种方法可以在DataTable中实现,但它听起来像是域对象的作业

class BoundDataObject
{
    public DateTime Field1 { get; set; }
    public short Field2 { get; set; }
    public bool PassFail
    {
        get { return Field1 < DateTime.Now.AddDays(Field2); }
    }
}
类BoundDataObject
{
公共日期时间字段1{get;set;}
公共短字段2{get;set;}
公共布尔PassFail
{
获取{returnfield1

将数据加载到域对象可能比数据表稍微费力,但这是一次性成本,我认为您会很快获得好处。

请参阅筛选器字符串中允许的表达式您的数据是从SQL检索的吗?是的,但存储在DataTable的内存中。请参阅筛选器字符串中允许的表达式您的数据是从SQL检索的吗?是的,但存储在DataTable的内存中。谢谢,但我正在搜索基于字符串的筛选器查询绑定数据表的BindingSource“Filter”属性。谢谢,但我正在搜索绑定数据表的BindingSource“Filter”属性的基于字符串的筛选器查询。