Format a Date when DateTime.MinValue

6 posts, 1 answers
  1. Bernard
    Bernard avatar
    41 posts
    Member since:
    Jun 2010

    Posted 27 Sep 2010 Link to this post

    Hello,
    I use a RadGrid with GridDateTileColumn. Sometime, I have DateTime.MinValue.
    I would like to display a Blank instead of 01/01/0001
    Is there a convenient way to do that ?
    Thanks a lot
  2. Answer
    Veli
    Admin
    Veli avatar
    2002 posts

    Posted 29 Sep 2010 Link to this post

    Hi Bernard,

    You can use RadGrid's ItemDataBound event for that. Suppose your DateTime field is named "Date", then you would have:

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            DateTime date = (DateTime)DataBinder.Eval(item.DataItem, "Date");
            if (date == DateTime.MinValue)
            {
                item["Date"].Text = "";
            }
        }
    }

    The above code will set an empty cell text for all rows that have a value of DateTime.MinValue for the Date field.

    Veli
    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. Bernard
    Bernard avatar
    41 posts
    Member since:
    Jun 2010

    Posted 30 Sep 2010 Link to this post

    Hi Veli,

    Many thanks for your answer, It's exactely what I was looking for...

    Regards
  4. mark baer
    mark baer avatar
    142 posts
    Member since:
    Sep 2009

    Posted 19 Mar 2012 Link to this post

    Has this solution changed?  Doesn't seem to be working for me:

     

     

     

     

    <P><FONT color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2"
    face="Consolas"><FONT color="#0000ff" size="2"
    face="Consolas"><</FONT></FONT></FONT><FONT color="#800000" size="2" face="Consolas"><FONT
    color="#800000" size="2" face="Consolas"><FONT color="#800000" size="2" face="Consolas">telerik</FONT></FONT></FONT><FONT
    color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2" face="Consolas">:</FONT></FONT></FONT><FONT color="#800000"
    size="2" face="Consolas"><FONT color="#800000" size="2" face="Consolas"><FONT
    color="#800000" size="2"
    face="Consolas">GridBoundColumn</FONT></FONT></FONT><FONT size="2" face="Consolas"><FONT
    size="2" face="Consolas"> </FONT></FONT><FONT color="#ff0000" size="2" face="Consolas"><FONT
    color="#ff0000" size="2" face="Consolas"><FONT color="#ff0000" size="2" face="Consolas">DataField</FONT></FONT></FONT><FONT
    color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2" face="Consolas">="Last
    Updated"</FONT></FONT></FONT><FONT size="2" face="Consolas"><FONT size="2" face="Consolas">
    </FONT></FONT><FONT color="#ff0000" size="2" face="Consolas"><FONT color="#ff0000"
    size="2" face="Consolas"><FONT color="#ff0000" size="2"
    face="Consolas">HeaderText</FONT></FONT></FONT><FONT color="#0000ff" size="2"
    face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff"
    size="2" face="Consolas">="Achieved?"</FONT></FONT></FONT><FONT size="2" face="Consolas"><FONT
    size="2" face="Consolas"> </FONT></FONT></P>
    <P><FONT size="2" face="Consolas"><FONT size="2"
    face="Consolas"> </FONT></FONT><FONT color="#ff0000" size="2" face="Consolas"><FONT
    color="#ff0000" size="2" face="Consolas"><FONT color="#ff0000" size="2" face="Consolas">UniqueName</FONT></FONT></FONT><FONT
    color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2"
    face="Consolas">="colLastUpdatedDate"</FONT></FONT></FONT><FONT size="2" face="Consolas"><FONT
    size="2" face="Consolas"> </FONT></FONT><FONT color="#ff0000" size="2" face="Consolas"><FONT
    color="#ff0000" size="2" face="Consolas"><FONT color="#ff0000" size="2" face="Consolas">DataFormatString</FONT></FONT></FONT><FONT
    color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2" face="Consolas">="{0:d}"</FONT></FONT></FONT><FONT
    size="2" face="Consolas"><FONT size="2" face="Consolas"> </FONT></FONT><FONT
    color="#ff0000" size="2" face="Consolas"><FONT color="#ff0000" size="2" face="Consolas"><FONT
    color="#ff0000" size="2"
    face="Consolas">FilterControlWidth</FONT></FONT></FONT><FONT color="#0000ff"
    size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2" face="Consolas">="60"</FONT></FONT></FONT><FONT size="2"
    face="Consolas"><FONT size="2" face="Consolas"> </FONT></FONT></P>
    <P><FONT size="2" face="Consolas"><FONT size="2"
    face="Consolas"> </FONT></FONT><FONT color="#ff0000" size="2" face="Consolas"><FONT
    color="#ff0000" size="2" face="Consolas"><FONT color="#ff0000" size="2" face="Consolas">DataType</FONT></FONT></FONT><FONT
    color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2"
    face="Consolas">="System.DateTime"></FONT></FONT></FONT></P>
    <P><FONT color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2"
    face="Consolas"><FONT color="#0000ff" size="2"
    face="Consolas"> </FONT></FONT></FONT><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><</FONT></FONT></FONT><FONT
    color="#800000" size="2" face="Consolas"><FONT color="#800000" size="2" face="Consolas"><FONT
    color="#800000" size="2" face="Consolas">HeaderStyle</FONT></FONT></FONT><FONT
    size="2" face="Consolas"><FONT size="2" face="Consolas"> </FONT></FONT><FONT
    color="#ff0000" size="2" face="Consolas"><FONT color="#ff0000" size="2" face="Consolas"><FONT
    color="#ff0000" size="2" face="Consolas">Width</FONT></FONT></FONT><FONT color="#0000ff"
    size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2" face="Consolas">="100px"</FONT></FONT></FONT><FONT
    size="2" face="Consolas"><FONT size="2" face="Consolas"> </FONT></FONT><FONT
    color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2" face="Consolas">/></FONT></FONT></FONT></P>
    <P><FONT color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2"
    face="Consolas"><FONT color="#0000ff" size="2"
    face="Consolas"> </FONT></FONT></FONT><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><</FONT></FONT></FONT><FONT
    color="#800000" size="2" face="Consolas"><FONT color="#800000" size="2" face="Consolas"><FONT
    color="#800000" size="2" face="Consolas">ItemStyle</FONT></FONT></FONT><FONT
    size="2" face="Consolas"><FONT size="2" face="Consolas"> </FONT></FONT><FONT
    color="#ff0000" size="2" face="Consolas"><FONT color="#ff0000" size="2" face="Consolas"><FONT
    color="#ff0000" size="2" face="Consolas">Width</FONT></FONT></FONT><FONT color="#0000ff"
    size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2" face="Consolas">="100px"</FONT></FONT></FONT><FONT
    size="2" face="Consolas"><FONT size="2" face="Consolas"> </FONT></FONT><FONT
    color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2" face="Consolas">/></FONT></FONT></FONT></P>
    <P><FONT color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2"
    face="Consolas"><FONT color="#0000ff" size="2"
    face="Consolas"> </FONT></FONT></FONT><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"></</FONT></FONT></FONT><FONT
    color="#800000" size="2" face="Consolas"><FONT color="#800000" size="2" face="Consolas"><FONT
    color="#800000" size="2" face="Consolas">telerik</FONT></FONT></FONT><FONT
    color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT
    color="#0000ff" size="2" face="Consolas">:</FONT></FONT></FONT><FONT color="#800000"
    size="2" face="Consolas"><FONT color="#800000" size="2" face="Consolas"><FONT
    color="#800000" size="2"
    face="Consolas">GridBoundColumn</FONT></FONT></FONT><FONT color="#0000ff" size="2"
    face="Consolas"><FONT color="#0000ff" size="2" face="Consolas"><FONT color="#0000ff"
    size="2" face="Consolas">></FONT></FONT></FONT></P>


    Here is my C# code...

     

     

     

    if (e.Item is GridDataItem) <BR> { <BR> //HyperLink editLink =
    (HyperLink)e.Item.FindControl("EditLink"); <BR> var editLink =
    (System.Web.UI.HtmlControls.HtmlAnchor)e.Item.FindControl("EditLink"); <BR>
    editLink.Attributes["href"] = "#"; <BR> //editLink.Attributes["onclick"] =
    String.Format("return ShowEditForm('{0}','{1}', '{2}', '{3}', '{4}');",
    e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ReportDueDateId"],
    e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["BoxId"],
    e.Item.ItemIndex, _authToken, _applicationApiKey); <BR>
    editLink.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}',
    '{2}', '{3}', '{4}');",
    e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"],
    e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["BoxId"],
    e.Item.ItemIndex, _authToken, _applicationApiKey); <BR> <BR> var deleteLink =
    (System.Web.UI.HtmlControls.HtmlAnchor)e.Item.FindControl("DeleteLink"); <BR>
    deleteLink.Attributes["href"] = "#"; <BR> //deleteLink.Attributes["onclick"] =
    String.Format("return confirmDeleteDueDate('{0}');",
    e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ReportDueDateId"]); <BR>
    deleteLink.Attributes["onclick"] = String.Format("return
    confirmDeleteDueDate('{0}');",
    e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"]); <BR> <BR> <BR>
    GridDataItem item = (GridDataItem)e.Item; <BR> <BR> DateTime date =
    (DateTime)DataBinder.Eval(item.DataItem, "LastUpdated"); <BR> <BR> if (date ==
    DateTime.MinValue) <BR> { <BR> <BR> item["LastUpdated"].Text = ""; <BR> <BR> }
    <BR> }<BR><BR><BR></FONT>

     

     


  5. Tsvetina
    Admin
    Tsvetina avatar
    2481 posts

    Posted 23 Mar 2012 Link to this post

    Hello Mark,

    The code provided by my colleague is still valid. Could you please repost yours, as the markup does not seem to correspond to your grid declaration and also some unwanted formatting appears in your server code.

    All the best,
    Tsvetina
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  6. Adhip
    Adhip avatar
    5 posts
    Member since:
    Aug 2016

    Posted 03 Sep 2017 in reply to Tsvetina Link to this post

    Hi Tsvetina,

    I'm searching for the same solution but for the kendo MVC Grid.

    Can you please help?

    Regards,

    Adhip

Back to Top