开发者

How to change the data in Telerik's RadGrid based on Calendar's selected dates?

开发者 https://www.devze.com 2022-12-21 06:01 出处:网络
I was creating another usercontrol with Telerik\'s RadGrid and Calendar. <%@ Register Assembly=\"Telerik.Web.UI\" Namespace=\"Telerik.Web.UI\" TagPrefix=\"telerik\" %>

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) {
 开发者_JS百科           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.


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消