如何根据日历的选定日期更改 Telerik 的 RadGrid 中的数据?

发布于 2024-08-22 17:17:01 字数 3886 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖迟 2024-08-29 17:17:01

当您将新数据源绑定到网格控件时,您需要调用Rebind以使网格显示新数据。或者,您可以使用 NeedDataSource 事件(我认为更优雅的解决方案)

对于您的第二个问题,请尝试此操作(假设您使用的是 ASP.NET Calendar 而不是 Telerik DatePicker:

if(Calendar1.SelectedDate.Date == DateTime.MinValue) {
    //no date selected
} else {
    //date is selected
}

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 use NeedDataSource event (more elegant solution in my opinion)

For your second problem try this (assuming you're using ASP.NET Calendar and not Telerik DatePicker:

if(Calendar1.SelectedDate.Date == DateTime.MinValue) {
    //no date selected
} else {
    //date is selected
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文