This is a migrated thread and some comments may be shown as answers.

Format a Date when DateTime.MinValue

5 Answers 888 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bernard
Top achievements
Rank 1
Bernard asked on 27 Sep 2010, 03:30 PM
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

5 Answers, 1 is accepted

Sort by
0
Accepted
Veli
Telerik team
answered on 29 Sep 2010, 03:11 PM
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
0
Bernard
Top achievements
Rank 1
answered on 30 Sep 2010, 09:03 AM
Hi Veli,

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

Regards
0
mark baer
Top achievements
Rank 1
answered on 19 Mar 2012, 09:18 PM
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>

 

 


0
Tsvetina
Telerik team
answered on 23 Mar 2012, 08:38 AM
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.
0
Adhip
Top achievements
Rank 1
answered on 03 Sep 2017, 01:22 PM

Hi Tsvetina,

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

Can you please help?

Regards,

Adhip

Tags
Grid
Asked by
Bernard
Top achievements
Rank 1
Answers by
Veli
Telerik team
Bernard
Top achievements
Rank 1
mark baer
Top achievements
Rank 1
Tsvetina
Telerik team
Adhip
Top achievements
Rank 1
Share this question
or