datetimepicker无法正确过滤datagridview数据

时间:2018-08-02 10:59:09

标签: c# winforms datagridview

我遇到一个导致过滤器无法正常工作的错误-也就是说,当我更改datetimepicker中的值时,datagridview变为空

我使用的代码是:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
   dataGridView1.DataSource = db.Expenses
                                .Where(x => x.Period == dateTimePicker1.Value)
                                .ToList();
}

2 个答案:

答案 0 :(得分:1)

尝试一下可能对您有帮助

 private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {
        dataGridView1.DataSource = db.Expenses
                     .Where(x => x.Period.Day == dateTimePicker1.Value.Day && 
                                 x.Period.Month == dateTimePicker1.Value.Month &&  
                                 x.Period.Year == dateTimePicker1.Value.Year)
                     .ToList();    
       //OR


       dataGridView1.DataSource = db.Expenses
                     .Where(x => x.Period.Date == dateTimePicker1.Value.Date)
                     .ToList();
    }

答案 1 :(得分:1)

可能在比较中包括了时间。只比较日期就能解决。

dataGridView1.DataSource = Expenses
             .Where(x => x.Period.Date == dateTimePicker1.Value.Date)
             .ToList();