我正在尝试过滤网格中同一列中的两个不同的(数据选择器)。我尝试用这两个字段来命令。整个过滤过程在后面的代码中进行,到目前为止,它正在为网格中的其他字段工作。但对于一个人,我得到一个奇怪的例外:
{“参数类型'Edm.DateTime'和'Edm.String'与此操作不兼容。 接近大于或等于表达式,第6行,第15列。“}
在数据库中,该字段是日期时间,它是单列
我的代码:
<telerik:GridBoundColumn DataField="EntryDate" DataType="System.DateTime" DataFormatString="{0:dd/MM/yyyy}" FilterControlAltText="Filter EntryDate column"
HeaderText="Date" SortExpression="EntryDate" UniqueName="EntryDate" FilterControlWidth="50px">
<FilterTemplate>
From
<telerik:RadDatePicker ID="FromOrderDatePicker" runat="server" Width="100px" AutoPostBack="true"
OnSelectedDateChanged="FromOrderDatePicker_SelectedDateChanged" OnPreRender="FromOrderDatePicker_PreRender" />
to
<telerik:RadDatePicker ID="ToOrderDatePicker" runat="server" Width="100px" AutoPostBack="true"
OnSelectedDateChanged="ToOrderDatePicker_SelectedDateChanged" OnPreRender="ToOrderDatePicker_PreRender" />
</FilterTemplate>
</telerik:GridBoundColumn>
代码背后:
protected void FromOrderDatePicker_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
{
RadDatePicker picker = sender as RadDatePicker;
ViewState["FromDatePicker"] = DateTimeToString(picker.SelectedDate.Value);
ExpressionChanged();
}
protected void FromOrderDatePicker_PreRender(object sender, EventArgs e)
{
if (ViewState["FromDatePicker"] != null)
{
RadDatePicker picker = sender as RadDatePicker;
picker.SelectedDate = StringToDateTime(ViewState["FromDatePicker"].ToString());
}
}
private string DateTimeToString(DateTime dt)
{
return dt.Month + "/" + dt.Day + "/" + dt.Year;
}
private DateTime StringToDateTime(string s)
{
string[] dts = s.Split('/');
DateTime dt = new DateTime(Int32.Parse(dts[2]), Int32.Parse(dts[0]), Int32.Parse(dts[1]));
return dt;
}
发布代码(对于toDatePicker)
是多余的 private void ExpressionChanged()
{
//reset
TimeReportGrid.MasterTableView.FilterExpression = null;
TimeReportGrid.MasterTableView.Rebind();
if (ViewState["FromDatePicker"] != null &&
ViewState["ToDatePicker"] != null &&
ViewState["FromDatePicker"].ToString() != "" &&
ViewState["ToDatePicker"].ToString() != "")
{
TimeReportGrid.MasterTableView.FilterExpression += "(it.EntryDate >= '" + ViewState["FromDatePicker"].ToString() + "') AND (it.EntryDate <= '" + ViewState["ToDatePicker"].ToString() + "')";
}
随时询问是否有任何问题
感谢您的帮助和快速回答!
答案 0 :(得分:1)
我的问题的答案非常简单我只需要声明EntryDate是DATETIME,看起来像那样
TimeReportGrid.MasterTableView.FilterExpression += "(it.EntryDate>= DATETIME '" + ViewState["FromDatePicker"].ToString() + " 00:00') AND (it.EntryDate<= DATETIME '" + ViewState["ToDatePicker"].ToString() + " 23:59')";
很简单,但我需要一些时间才能得到它
随意询问是否有任何问题