GreatGatsby
GreatGatsby

Reputation: 75

Change datetimepicker displayed date

I am using Visual Studio 2010 C# and I would like to change how datetimepicker's textbox displays the date I pick.

By default, it is for example 13-Jul-2013. I want to change it in yyyy-mm-dd format because i want to store this in database column with date values.

I tried the following code but I get "Object reference not set to an instance of an object." error.

  datetimepicker1.Format = DateTimePickerFormat.Custom;
  datetimepicker1.CustomFormat = "yyyy-mm-dd";

Any ideas how to fix this?

Upvotes: 0

Views: 188

Answers (1)

user2509901
user2509901

Reputation:

I think the problem is how your datetimepicker is created. Just Create Your Own at runtime using

DateTimePicker dateTimePicker1 = new DateTimePicker();
public Form1()
{
    InitializeComponent();
    dateTimePicker1.Format = DateTimePickerFormat.Custom;
    dateTimePicker1.CustomFormat = "yyyy-mm-dd";
    //You can play with this to change location
    dateTimePicker1.Location = new Point(20, 20);
    this.Controls.Add(dateTimePicker1);
}

Upvotes: 1

Related Questions