Earlier this week while working on another blog entry, I ran into a unique situation involving the RadGridView. Basically, what I am doing is retrieving user status data from Facebook using the Facebook Developer Toolkit. The data is returned in a list of objects based on the Facebook API schema. These objects are of type user_status and contain the following:

uuid - long
status_id - long
time - long
source - string
message - string
comment_count - int

I basically wanted to take this list and bind it to the RadGridView showing only the message and the time for that message. This was pretty easy to do using basic DataBinding. I've created an example of this situation using my own custom objects and test data:

List<UserStatus> _statusList;
public Form1()
    // Create the data
    _statusList = new List<UserStatus>();
    _statusList.Add(new UserStatus(1263483153, "This is my status"));
    _statusList.Add(new UserStatus(1263473153, "This is my status"));
    _statusList.Add(new UserStatus(1262463153, "This is my status"));
    _statusList.Add(new UserStatus(1261453153, "This is my status"));
    _statusList.Add(new UserStatus(1260443153, "This is my status"));
    _statusList.Add(new UserStatus(1263433153, "This is my status"));
    _statusList.Add(new UserStatus(1263423153, "This is my status"));
    _statusList.Add(new UserStatus(1263413153, "This is my status"));
    _statusList.Add(new UserStatus(1263403153, "This is my status"));
    _statusList.Add(new UserStatus(1263400000, "This is my status"));
private void btnPopulate_Click(object sender, EventArgs e)
    userStatusBindingSource.DataSource = _statusList;


As you can see in my test data, the time values are giant number values?! Why is this? Well, the reason for this is that Facebook is actually returning times in Unix time. Unix time is the number of seconds that has elapsed since January 1st, 1970. You can read more about this here. Displaying this data is no good to me, nobody is going to want to calculate the date of a status update in their head using the number of seconds. Luckily, the RadGridView is equipped to handle this type of situation via the CellFormatting event.

private void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
    GridViewDataColumn column = e.CellElement.ColumnInfo as GridViewDataColumn;
    if (column.UniqueName == "UnixTime")
        long value = (long)((GridDataCellElement)e.CellElement).Value;
        DateTime date = GetDateTimeFromTimestamp(value);
        e.CellElement.Text = date.ToShortDateString();
private DateTime GetDateTimeFromTimestamp(long timestamp)
    DateTime date_time = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return date_time.AddSeconds(timestamp).AddHours(-8).ToUniversalTime();


Using the CellFormatting event, I can easily retrieve the current value of a particular cell, i.e. Unix time, and format it to be displayed in human readable DateTime format. All without changing the value stored in the underlying datasource. This saves me some time since I now don't have to convert the object manually, use a wrapper class, or populate the grid values manually. Pretty useful eh?

Click here to download the source code used in this post.

Related Posts


Comments are disabled in preview mode.