2010-02-24 81 views
1

我正在用Telerik的RadGrid和Calendar創建另一個用戶控件。如何根據日曆的選定日期更改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是不足夠的。 =(

感謝。

回答

3

當您綁定一個新的數據源,你需要調用Rebind有網格顯示新數據的網格控件。另外,您可以使用NeedDataSource事件(在我看來,更優雅的解決方案)

你的第二個問題,試試這個(假設你使用ASP.NET日曆和不Telerik的的DatePicker:

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

我不熟悉這些功能=( – Jronny 2010-02-24 07:21:47

+1

只需添加'RadGrid1.Rebind() ;'r在你的'BindData'方法中'RadGrid1.DataSource = DataSource'後面的ight – 2010-02-24 07:22:29

+0

它正常工作!謝謝。 – Jronny 2010-02-24 07:37:57

相關問題