3

How can I split "DateTime" column from database, which has the format yyyy-mm-dd 00:00:00.000, into two separate columns of a RadGrid?

i.e., in Date column of RadGrid, Date should display from database DateTime column.
And in Time column of RadGrid, Time should display from database DateTime column.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
timz_123
  • 435
  • 1
  • 9
  • 47

1 Answers1

3

You can bind the very same field to two different bound columns (or whatever the column types you've used).

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

Edit: I think use of IEnumerable<T> is the best approach to solve number of issues including data formatting and checking empty fields.

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
}

Write code to populate the IEnumerable<MyModel> data-source which will be used by the grid.

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 );
...
...

and finally bind the list data source to your Grid control.

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

Update suggested by OP

Below code is also working fine to check the null/default values before display it into 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 = "";
            }
        }
   }
Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • I get below error: `Multiple controls with the same ID 'FilterTextBox_UpdateDateTime' were found. FindControl requires that controls have unique IDs.` Please reply what shall I change in my code ? – timz_123 Aug 17 '15 at 04:03
  • @user3196511 I think you've to set different value for `UniqueName`. – KV Prajapati Aug 17 '15 at 04:32
  • Thank you for the reply. Its working fine now after changing the `UniqueName`. 1 more thing I want to ask that If some records do not have DateTime in Database table, then in RadGrid, its showing `01/01/1900` for Date and `00:00:00' for Time. Please let me know how show empty/blank cell in place of the default date time coming for few columns ? Please reply – timz_123 Aug 17 '15 at 04:46
  • In Item_databound event, check for empty fields using `if` condition and if it empty, then make it to display your own text. @user3196511 – Balaji Aug 17 '15 at 04:51
  • @Balaji: Thank you for the reply. Can you please let me know how to check for empty fields for 2 separate date and time columns of Grid, in which data is coming from 1 "DateTime" column of Database table ? Please reply – timz_123 Aug 17 '15 at 06:15
  • 1
    Thank you both of you for the help. I too added my solution to the post above and its working fine. – timz_123 Aug 17 '15 at 06:58