How to set the default value of DateTime to empty string?
Asked Answered
M

4

3

I have a property called Raised_Time, this property shows the time at which alarm is raised in datagrid Cell. I don't want to show anything in the datagrid cell when user creates any alarm, it just display the empty cell.

I googled in the internet and found that the default value of DateTime can be set using DateTime.MinValue and this will display MinValue of datetime i:e "1/1/0001 12:00:00 AM".

Instead I want that the datagrid cell remain blank until alarm is raised, it don't show any time.

I think datatrigger can be written in this case. I am not able to write the datatrigger for this scenario. Do I need a converter also that checks if DateTime is set to DateTime.MinValue the leave the datagrid cell blank??

Please help!!

Morissa answered 7/9, 2010 at 12:6 Comment(1)
Googling in the internet.. Nice ;)Saprophyte
B
3

How about just changing your property to link to a private field of DateTime e.g.:

public string Raised_Time
{
  get
  {
    if(fieldRaisedTime == DateTime.MinValue)
    {
      return string.Empty();
    }
    return DateTime.ToString();
  }
  set
  {
    fieldRaisedTime = DateTime.Parse(value,   System.Globalization.CultureInfo.InvariantCulture);
  }
}
Boris answered 7/9, 2010 at 12:18 Comment(0)
A
9

I would use a Converter for this because it's something I can easily see reusing in the future. Here's one I used to use that took a string value of the DateFormat as the ConverterParameter.

public class DateTimeFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((DateTime)value == DateTime.MinValue)
            return string.Empty;
        else
            return ((DateTime)value).ToString((string)parameter);
    }


    public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }
}
Aurify answered 7/9, 2010 at 12:22 Comment(0)
L
7

I see two easy options to solve this:

  1. You use the Nullable data type DateTime?, so that you can store null instead of DateTime.MinValue if the alarm time is not set.

  2. You can use a converter, here is an example.

Lentiginous answered 7/9, 2010 at 12:21 Comment(0)
B
3

How about just changing your property to link to a private field of DateTime e.g.:

public string Raised_Time
{
  get
  {
    if(fieldRaisedTime == DateTime.MinValue)
    {
      return string.Empty();
    }
    return DateTime.ToString();
  }
  set
  {
    fieldRaisedTime = DateTime.Parse(value,   System.Globalization.CultureInfo.InvariantCulture);
  }
}
Boris answered 7/9, 2010 at 12:18 Comment(0)
C
2

I use a nullable datetime for this, with an extension method like:

 public static string ToStringOrEmpty(this DateTime? dt, string format)
 {
     if (dt == null)
        return string.Empty;

     return dt.Value.ToString(format);
 }
Candiot answered 7/9, 2010 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.