2015-08-17 30 views

回答

3

您可以将相同的字段绑定到两个不同的绑定列(或任何您使用的列类型)。

<telerik:GridBoundColumn 
    DataField="YourDateField" 
    UniqueName="CDate" 
    HeaderText="Date" 
    DataType="System.DateTime" DataFormatString="{0:yyyy-MM-dd}"> 
</telerik:GridBoundColumn> 
<telerik:GridBoundColumn 
    DataField="YourDateField" 
    UniqueName="CTime" 
    HeaderText="Time" 
    DataType="System.DateTime" DataFormatString="{0:HH:mm:ss}"> 
</telerik:GridBoundColumn> 

编辑: 我想使用的IEnumerable<T>是解决问题,包括数据格式和检查空字段数的最佳方法。

public class MyModel 
{ 
    public string UpdateDate {get;set;} 
    public string UpdateTime {get;set;} 
//and add other properties which you need to be a part of grid 
} 

编写代码填充将由网格使用的数据源IEnumerable<MyModel>

var list = new List<MyModel>(); 

// Read one by one row/result from database and set value to 
// an instance of MyModel 

/* read/fetch row from database */ 
..... 
var dbRow = /* fetch a row */ 
var model = new MyModel{ UpdateTime = "", UpdateDate=""}; 
if(dbRow.UpdateDateTime !=null) 
{ 
    model.UpdateDate = dbRow.UpdateDateTime.ToString("yyyy-MM-dd"); 
    model.UpdateTime = dbRow.UpdateDateTime.ToString("HH:mm:ss"); 
} 
list.Add(model); 
... 
... 

最后绑定list数据源到您的Grid控制。

SO主题 - How Handle Null Values(In Columns) In Telerik RadGrid?

更新由OP建议

下面的代码也工作正常,检查显示之前的空/默认值到radgrid控件:

protected void GridReport_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
     if (e.Item is GridDataItem) 
     { 
      GridDataItem item = (GridDataItem)e.Item; 

      if (item["ScanDate"].Text == "01/01/1900") 
      { 
       item["ScanDate"].Text = ""; 
      } 

      if(item["ScanTime"].Text == "00:00:00 AM") 
      { 
       item["ScanTime"].Text = ""; 
      } 
     } 
    } 
+0

我得到以下错误: '找到了具有相同ID'FilterTextBox_UpdateDateTime'的多个控件。 FindControl要求控件具有唯一的ID。# 请回复我在代码中更改的内容? – user3196511

+0

@ user3196511我认为你必须为'UniqueName'设置不同的值。 – adatapost

+0

谢谢你的回复。在更改'UniqueName'后它现在工作正常。另外1件事我想问,如果某些记录在数据库表中没有DateTime,那么在RadGrid中,它显示日期为'01/01/1900',时间为'00:00:00'。请让我知道如何显示空白/空白单元格来代替几列的默认日期时间?请回复 – user3196511

相关问题