How to Set Empty String in "System.DateTime?" The nullable DateTimeType.

S

Sugandh Jain

Hi,

I am using property like this..
private DateTime? _propName;

public DateTime? PropName

{

get { return _propName; }

set { _propName = value; }

}

Now, the objects of this properties parent Type are shown in a grid in our
application.

This date is populated when user does some action. When no action has been
taken, it is fetched as null for the database.

Now, In the grid I want to show it as empty string, but right now, it shows
it as 0.

How do I get this nullable reference type to represent an empty string?/



Regards,

Sugandh
 
M

Marc Gravell

How do I get this nullable reference type to represent an empty

Actually, that's a nullable value type, which is itself a value type.
However, it all looks fine for me (see below) - but I guess it comes
down to the format options on the columns, assuming that you are using
proper TypeConverter usage and not just calling .ToString() yourself:

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Diagnostics;

class MyData {
private DateTime? a, b, c;
public DateTime? A { get { return a; } set { a = value; } }
public DateTime? B { get { return b; } set { b = value; } }
public DateTime? C { get { return c; } set { c = value; } }

public MyData() {
A = null; // null
B = new DateTime(); // non-null zero
C = DateTime.Now; // non-zero
}
}

class MyProgram {

static void Main() {
BindingList<MyData> list = new BindingList<MyData>();
list.Add(new MyData());
using (Form f = new Form())
using (DataGridView dgv = new DataGridView()) {
dgv.AutoGenerateColumns = true;
dgv.Dock = DockStyle.Fill;
dgv.DataSource = list;
f.Controls.Add(dgv);
Application.Run(f);
}
}
}
 
S

Sugandh Jain

Thanks I got it. Set the Null Text for the column in the grid to empty
string. and left the value of the volumn to be null.
So, it does not show a default date, but the empty string..

Thanks,
Sugandh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top