Blog Stats
  • Posts - 14
  • Articles - 0
  • Comments - 6
  • Trackbacks - 0

 

One way to have Telerik RadGridView control show blank value for a null value in DateTime column

For whatever reason of which the Fates may only know, we experienced a difference in pulling data manually as a list of objects than we did in pulling a list of objects from an entity framework.  The current work around to get an empty value in the null datetime field was to do the following in the RadGridView's CellFormatting event.  We are using RadControls for Winforms 2011 Q1.

Oddly enough, as mentioned above, pulling the data as a list of objects from an entity framework does not have to have this work around.  We tried having DateTime? MyDateField in the field list of the object without the code below.  It would display '01/01/0001' for each null value in the DateTime column of the grid.

So the measure below is in place.  It is very quick and does not cause a lot of overhead.  Still I would prefer a better solution if someone has fought this before.

private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
   if (e.CellElement != null)
   {
      GridViewDataColumn dataColumn = e.CellElement.ColumnInfo as GridViewDataColumn;
 
      if (dataColumn.Name == "MyDateField")
      {
         DateTime dtValue = (DateTime)e.CellElement.RowInfo.Cells[dataColumn.Name].Value;
         if (dtValue == System.DateTime.MinValue)
         {
            e.CellElement.Text = "";
         }
      }
   }
 }

Feedback

# re: One way to have Telerik RadGridView control show blank value for a null value in DateTime column

Gravatar Before even reading your post in full I'd assumed setting value to empty string would do the trick. This seems to be their de facto method for dealing with anything where a value, or even records in datasource, is not present.

I have not used the WinForms version of their RadControls, but having used the ASP.NET RadControls one had to follow the same rules.

Anyway, nice catch! 4/24/2012 10:33 PM | Jake Rutherford

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

 

 

Copyright © DavidMadden