如何根据日历的选定日期更改 Telerik 的 RadGrid 中的数据?
我正在使用 Telerik 的 RadGrid 和日历创建另一个用户控件。
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<table class="style1">
<tr>
<td>From</td>
<td>To</td>
</tr>
<tr>
<td><asp:Calendar ID="Calendar1" runat="server" SelectionMode="Day"></asp:Calendar></td>
<td><asp:Calendar ID="Calendar2" runat="server" SelectionMode="Day"></asp:Calendar></td>
</tr>
<tr>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></td>
<td><asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" /></td>
</tr>
</table>
<telerik:RadGrid ID="RadGrid1" runat="server">
<MasterTableView CommandItemDisplay="Top"></MasterTableView>
</telerik:RadGrid>
我在代码隐藏中使用 Linq:
Entities1 entities = new Entities1();
public static object DataSource = null;
protected void Page_Load(object sender, EventArgs e) {
if (DataSource == null) {
DataSource = (from entity in entities.nsc_moneytransaction
select new {
date = entity.transaction_date.Value,
username = entity.username,
cashbalance = entity.cash_balance
}).OrderByDescending(a => a.date);
}
BindData();
}
public void BindData() {
RadGrid1.DataSource = DataSource;
}
protected void btnSubmit_Click(object sender, EventArgs e) {
DateTime startdate = new DateTime();
DateTime enddatedate = new DateTime();
if (Calendar1.SelectedDate != null && Calendar2.SelectedDate != null) {
startdate = Calendar1.SelectedDate;
enddatedate = Calendar2.SelectedDate;
var queryDateRange = from entity in entities.nsc_moneytransaction
where DateTime.Parse(entity.transaction_date.Value.ToShortDateString())
>= DateTime.Parse(startdate.ToShortDateString())
&& DateTime.Parse(entity.transaction_date.Value.ToShortDateString())
<= DateTime.Parse(enddatedate.ToShortDateString())
select new {
date = entity.transaction_date.Value,
username = entity.username,
cashbalance = entity.cash_balance
};
DataSource = queryDateRange.OrderByDescending(a => a.date);
} else if (Calendar1.SelectedDate != null) {
startdate = Calendar1.SelectedDate;
var querySetDate = from entity in entities.nsc_moneytransaction
where entity.transaction_date.Value == startdate
select new {
date = entity.transaction_date.Value,
username = entity.username,
cashbalance = entity.cash_balance
};
DataSource = querySetDate.OrderByDescending(a => a.date); ;
}
BindData();
}
protected void btnClear_Click(object sender, EventArgs e) {
Calendar1.SelectedDates.Clear();
Calendar2.SelectedDates.Clear();
}
问题是,(1) 当我单击提交按钮时。 RadGrid 中的数据未更改。 (2) 我们如何检查日历控件中是否没有选择任何内容,因为即使我们没有从该日历中选择任何内容,也设置了一个日期 (01/01/0001),因此 Calendar1.SelectedDate != null 是不够的。 =(
谢谢。
I was creating another usercontrol with Telerik's RadGrid and Calendar.
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<table class="style1">
<tr>
<td>From</td>
<td>To</td>
</tr>
<tr>
<td><asp:Calendar ID="Calendar1" runat="server" SelectionMode="Day"></asp:Calendar></td>
<td><asp:Calendar ID="Calendar2" runat="server" SelectionMode="Day"></asp:Calendar></td>
</tr>
<tr>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></td>
<td><asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" /></td>
</tr>
</table>
<telerik:RadGrid ID="RadGrid1" runat="server">
<MasterTableView CommandItemDisplay="Top"></MasterTableView>
</telerik:RadGrid>
and I am using Linq in code-behind:
Entities1 entities = new Entities1();
public static object DataSource = null;
protected void Page_Load(object sender, EventArgs e) {
if (DataSource == null) {
DataSource = (from entity in entities.nsc_moneytransaction
select new {
date = entity.transaction_date.Value,
username = entity.username,
cashbalance = entity.cash_balance
}).OrderByDescending(a => a.date);
}
BindData();
}
public void BindData() {
RadGrid1.DataSource = DataSource;
}
protected void btnSubmit_Click(object sender, EventArgs e) {
DateTime startdate = new DateTime();
DateTime enddatedate = new DateTime();
if (Calendar1.SelectedDate != null && Calendar2.SelectedDate != null) {
startdate = Calendar1.SelectedDate;
enddatedate = Calendar2.SelectedDate;
var queryDateRange = from entity in entities.nsc_moneytransaction
where DateTime.Parse(entity.transaction_date.Value.ToShortDateString())
>= DateTime.Parse(startdate.ToShortDateString())
&& DateTime.Parse(entity.transaction_date.Value.ToShortDateString())
<= DateTime.Parse(enddatedate.ToShortDateString())
select new {
date = entity.transaction_date.Value,
username = entity.username,
cashbalance = entity.cash_balance
};
DataSource = queryDateRange.OrderByDescending(a => a.date);
} else if (Calendar1.SelectedDate != null) {
startdate = Calendar1.SelectedDate;
var querySetDate = from entity in entities.nsc_moneytransaction
where entity.transaction_date.Value == startdate
select new {
date = entity.transaction_date.Value,
username = entity.username,
cashbalance = entity.cash_balance
};
DataSource = querySetDate.OrderByDescending(a => a.date); ;
}
BindData();
}
protected void btnClear_Click(object sender, EventArgs e) {
Calendar1.SelectedDates.Clear();
Calendar2.SelectedDates.Clear();
}
The problems are, (1) when I click the submit button. the data in the RadGrid is not changed. (2) how can we check if there is nothing selected in the Calendar controls, because there is a date (01/01/0001) set even if we do not select anything from that calendar, thus Calendar1.SelectedDate != null is not enough. =(
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将新数据源绑定到网格控件时,您需要调用
Rebind
以使网格显示新数据。或者,您可以使用NeedDataSource
事件(我认为更优雅的解决方案)对于您的第二个问题,请尝试此操作(假设您使用的是 ASP.NET Calendar 而不是 Telerik DatePicker:
When you bind a new datasource to the grid control you need to call
Rebind
to have the grid show the new data. Alternatively you could useNeedDataSource
event (more elegant solution in my opinion)For your second problem try this (assuming you're using ASP.NET Calendar and not Telerik DatePicker: