Handling a null datetime in a grid using TimeZoneID="CentralStandardTime"

0 Answers 36 Views
Grid Localization
Allan Erickson
Top achievements
Rank 1
Allan Erickson asked on 19 Jan 2023, 04:33 PM

We have multiple RadGrids with the MasterTableView configured thusly:

<MasterTableView EditFormSettings-PopUpSettings-KeepInScreenBounds="true" EditMode="PopUp" TimeZoneID="Central Standard Time">

If a record from the database has a null datetime value, the min value of DATETIME data type is used (01/01/0001). When the grid tries convert UTC to CST, we get the error: The added or subtracted value results in an un-representable DateTime.
Parameter name: value.

How can this be handled so the Grid display an empty cell when a null datetime is encountered?

Rumen
Telerik team
commented on 24 Jan 2023, 11:51 AM | edited

Hi Allan,

Yes, the error indicates that RadGrid is trying to convert a null DateTime value from UTC to CST, which is resulting in an unrepresentable DateTime.

One solution to handle the DateTime null values in your database query before they are passed to the RadGrid. You can do this by using a coalesce function or a case statement in your query to convert the DateTime null values to an empty string or a default value of your choice before they are loaded and displayed in the grid.

Another approach is to attach to the RadGrid OnItemDataBound server event and to check for null DateTime values, and when found, set the cell's value to an empty string or some default value:
ASPX

 

<telerik:RadGrid ID="RadGrid1" runat="server" OnItemDataBound="RadGrid1_ItemDataBound">
    <MasterTableView EditFormSettings-PopUpSettings-KeepInScreenBounds="true" EditMode="PopUp" TimeZoneID="Central Standard Time">
        ...
    </MasterTableView>
</telerik:RadGrid>

 

C#

 

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        DateTime dateValue = Convert.ToDateTime(item["DateTimeColumnName"].Text);
        if (dateValue == DateTime.MinValue)
        {
            item["DateTimeColumnName"].Text = string.Empty;
        }
        else
        {
            DateTime cstDatetime = TimeZoneInfo.ConvertTime(dateValue, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"));
            item["DateTimeColumnName"].Text = cstDatetime.ToString();
        }
    }
}

 

No answers yet. Maybe you can help?

Tags
Grid Localization
Asked by
Allan Erickson
Top achievements
Rank 1
Share this question
or