VB.NET Error: "Operand Missing" when using RowFilter Command

VB.NET Error: "Operand Missing" when using RowFilter Command

我正在尝试根据两个不同的列过滤数据视图:StartTime(DateTime) 和 EndTime(DateTime)...

代码:

 Dim dvLosses As New DataView(dsLossData.Tables("AllData").DefaultView.ToTable(True, New String(){"ID", "Name", "StartTime", "EndTime", "Loss"}), "", "StartTime desc", DataViewRowState.CurrentRows)

 dvLosses.RowFilter = "Where StartTime > '" + hfFrom.Value + "' and EndTime < '" + hfTo.Value + "'"

Error: Exception details: Syntax error: Missing operand after 'StartTime' operator. at System.Data.ExpressionParser.Parse() at System.Data.DataExpression..ctor(DataTable table, String expression, Type type) at System.Data.DataView.set_RowFilter(String value)

RowFilter 属性 是一个或多或少类似于 WHERE 表达式的表达式,但它不需要单词 WHERE。只需输入条件表达式

 dvLosses.RowFilter = "StartTime > '" + hfFrom.Value + 
                      "' and EndTime < '" + hfTo.Value + "'"

相反,我更关心的是,您将两个看似日期时间的字段视为字符串。如果它们真的是日期时间字段,那么您需要不同的语法和引号字符

 dvLosses.RowFilter = "StartTime > #" + hfFrom.Value.ToString("M/d/yyyy") + 
                      "# and EndTime < #" + hfTo.Value.ToString("M/d/yyyy") + "#"

(假设你hfFrom.Value和hfTo.Value是DateTime变量)

您可以在 DataColumn

Expression 属性 中找到有关 RowFilter 属性 接受的语法的更多信息