Null date values in GridBoundColumn

5 posts, 1 answers
  1. Rich
    Rich avatar
    25 posts
    Member since:
    Jun 2010

    Posted 05 Nov 2010 Link to this post

    Hi,

    For displaying datetime values in a RadGrid column I am using a GridBoundColumn with DataType="System.DateTime" and DataFormatString="{0:M/d/yyyy}".  This works fine except in cases where the bound value is null (DateTime.MinVal) - the date value is displayed as "1/1/0001". 

    I could define the column as a string and emit a blank string when the date field is set to DateTime.MinVal, but then I loose the capability of sorting on the date column (unless the date format is "yyyy/MM/dd".

    Is there a way to leave the data type of the column as "DateTime" and also display an empty cell if the value of the date is DateTime.MinVal?
  2. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 09 Nov 2010 Link to this post

    Hello Richard,

    To achieve the desired functionality you could try using the following code snippet into the RadGrid.ItemDataBound event handler:
    void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            if (item["Date"].Text == DateTime.MinValue.ToString("M/d/yyyy"))
            {
                item["Date"].Text = "";
            }
        }
    }

    Please give it try and let me know if it helps you.

    Greetings,
    Radoslav
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jeremy
    Jeremy avatar
    1 posts
    Member since:
    Nov 2010

    Posted 09 Nov 2010 Link to this post

    The proposed solution is not working for linkbutton columns. 

    The markup from the column is shown below and a graphic of the web page is attached:

        <telerik:GridButtonColumn
            ButtonType="LinkButton"
            UniqueName="AssignmentDate"
            CommandName="AssignmentDate" 
            DataTextField="AssignmentDate"
            DataTextFormatString="{0:M/d/yyyy}"
            DataType="System.DateTime"
            HeaderText="Assign Date"
            ItemStyle-HorizontalAlign="Center" />

  5. Answer
    Dimo
    Admin
    Dimo avatar
    8330 posts

    Posted 11 Nov 2010 Link to this post

    Hello,

    The LinkButton's Text should be manipulated, instead of the TableCell's one:

    Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
        If TypeOf e.Item Is GridDataItem AndAlso Not e.Item.IsInEditMode Then
                Dim cell As TableCell = DirectCast(e.Item, GridDataItem)("Date1")
                Dim lb As LinkButton = DirectCast(cell.Controls(0), LinkButton)
                If lb.Text = "1/1/0001" Then
                    cell.Text = "&nbsp;"
                End If
        End If
    End Sub


    Regards,
    Dimo
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Rich
    Rich avatar
    25 posts
    Member since:
    Jun 2010

    Posted 11 Nov 2010 Link to this post

    That fixed the problem - thank you
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017