Viviannne
Viviannne

Reputation: 121

datetimepicker select a value after dropdown instead of on dropdown C#

I have an datetimepicker issue on winform. When the form loads, I initializes the datetimepicker as

DateIdentifier.Format = DateTimePickerFormat.Custom;
DateIdentifier.CustomFormat = " ";
DateIdentifier.Value = DateTime.Now;

then, it retrieves value from datagridview which in turn has datasource from a backend table, like this:

if (Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["DateIdentified"].Value) == "")
{
    DateIdentifier.CustomFormat = " ";
    DateIdentifier.Text = null;
}
else
{
   DateIdentifier.CustomFormat = "yyyy-MM-dd";
   DateIdentifier.Text = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["DateIdentified"].Value);
}

it works fine so far, but when I click on the drop down button, the current datetime stamp will overwrites whatever date time retrieved from the database, I guess it's because I set DateIdentifier.Value = DateTime.Now. Is there some way to maintain the textbox of the datetimepicker showing value from datagridview when dropdown is hit and only when I truly select a date in the accompany calendar, the new picked date will be showed in the textbox of the datetimepicker?

Thanks.

Upvotes: 0

Views: 1359

Answers (1)

jvanrhyn
jvanrhyn

Reputation: 2824

You are not setting the selectedDate of the datetime picker.

   if (Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["DateIdentified"].Value) == "")
    {
        DateIdentifier.CustomFormat = " ";
        DateIdentifier.Text = null;
    }
    else
    {
       DateIdentifier.CustomFormat = "yyyy-MM-dd";
       DateIdentifier.SelectedValue = DateTime.Parse(dataGridView1.Rows[e.RowIndex].Cells["DateIdentified"].Value);
    DateIdentifier.Text = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["DateIdentified"].Value);
    }

Upvotes: 2

Related Questions