2012-06-12 169 views
0

我需要更改單元格的顏色比較日期 - 這是依賴於一個日期 - 我可以改變細胞的顏色在這裏沒有問題在GridDataItem細胞以顏色細胞

 GridDataItem item = e.Item as GridDataItem; 

     **if ((item["run_Date"].Text > DateTime.Now))** //error is in this line of code 
     { 
      foreach (GridColumn col in radgrdResultDetail.MasterTableView.Columns) 
      { 
       item["run_Date"].BackColor = Color.FromArgb(255, 106, 106); 

      } 
     } 

但其努力訪問「日期」 - 我是在我的錯誤 來我已經附上了我的e.item

enter image description here

回答

1

什麼是您收到的確切的錯誤消息的圖像?我的假設是,您正在將String cell.Text與DateTime對象DateTime.Now進行比較,從而得到無效的轉換異常。嘗試將文本轉換爲datetime對象是這樣的:

if(DateTime.Compare(Convert.ToDateTime(item["run_date"].Text), DateTime.Now) > 0) 

還是看細胞本身,我想返回一個對象,並把它轉換爲datetime對象

if(DateTime.Compare((DateTime)item["run_date"], DateTime.Now) > 0) 
+0

嗨馬特 - 我得到的問題「無法找到一個單元格綁定到列名'run_Date'」 – MMC

+0

是否使用.Text屬性的第一行工作?如果不是這是一個模板字段/單元格?如果它是一個模板化的字段/單元格,您將需要找到保存該值的實際控件。例如TextBox tBox =(TextBox)item [「run_date」]。FindControl(「<模板中顯示控件的名稱>」); 幾個環節可能會有所幫助: [Telerik的論壇](http://www.telerik.com/community/forums/aspnet-ajax/grid/316967-griddataitem.aspx) [Telerik的幫助下訪問細胞和行](http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html) – Matt

+0

固定 - 謝謝你們 – MMC

1

試試這個

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
{ 
    //Is it a GridDataItem 
    if (e.Item is GridDataItem) 
    { 
     //Get the instance of the right type 
     GridDataItem item= e.Item as GridDataItem; 

     //Check the formatting condition 
     if(DateTime.Compare(Convert.ToDateTime(item["run_date"].Text), DateTime.Now) > 0) 
     { 
      item["run_Date"].BackColor = Color.FromArgb(255, 106, 106); 
      //Customize more... 
     } 
    } 
} 

編號:
Conditional formatting for rows/cells on ItemDataBound
How to change row color for RadGrid with specified conditions?