0

我的 DGV 中有这个日期:

14/06/2012
15/07/2012
16/07/2012
17/07/2012

我将过滤器设置为:([supplier_invoice_date] >= '13/07/2012') AND ([supplier_invoice_date] <= '17/07/2012')

过滤器返回此(所有日期)

14/06/2012
15/07/2012
16/07/2012
17/07/2012

另一个测试:

筛选:([supplier_invoice_date] >= '15/07/2012') AND ([supplier_invoice_date] <= '17/07/2012')

结果:

    15/07/2012
    16/07/2012
    17/07/2012

筛选:([supplier_invoice_date] < '17/06/2012')

结果:

14/06/2012
15/07/2012
16/07/2012

我认为只需要几天而不需要几个月。

这是我的代码:

Dim dt As New DataTable
Dim suppliersinvoices_data_query As String = ("DATE_FORMAT(MIN(supplier_invoice_date), '%d/%m/%Y') AS supplier_invoice_date, ...")
Dim invoice_objDataAdapter As New MySqlDataAdapter(suppliersinvoices_data_query, objConn)
invoice_objDataAdapter.Fill(dt)

Dim MyFilter As New DataView(dt)
MyFilter.RowFilter = "([supplier_invoice_date] >= '13/07/2012') AND ([supplier_invoice_date] <= '17/07/2012')"
invoicesresults_datagrid_search_supplierinvoice.DataSource = MyFilter
4

2 回答 2

1

看起来您正在将日期转换为字符串以格式化它们。尽量避免这种情况,让网格使用它从数据源获取的日期值。

如果要格式化 DataGridView 控件的日期,可以这样尝试:

dgv1.Columns("YourDateColumn").DefaultCellStyle.Format = "dd/MM/yyyy"

然后您可以使用真实日期来过滤数据:

MyFilter.Filter = String.Format("[supplier_invoice_date] > '{0}'", _
                                New DateTime(2012, 7, 1))
于 2012-07-17T21:58:49.340 回答
1

这是DataView.RowFilter语法for日期:

dataView.RowFilter = "supplier_invoice_date >= #2012-07-13#"

但你也可以使用Linq-To-DataSet. 我假设该字段的实际数据类型是String而不是Date,因此您需要Date先将其解析为:

Dim startDate = New Date(2012, 7, 13)
Dim endDate =   New Date(2012, 7, 17)
Dim invoiceDate As Date
Dim filtered = From row In dt.AsEnumerable()
    Where Date.TryParse(row.Field(Of String)("supplier_invoice_date"), invoiceDate) _
    AndAlso invoiceDate >= startDate AndAlso invoiceDate <= endDate
Dim tblFiltered As DataTable = filtered.CopyToDataTable()
于 2012-07-17T22:03:02.347 回答