DateTimePicker displays today's date instead of displaying its actual Value - c#

We have a couple of DateTimePickers on a custom UserControl, on a Form. They are visible, but not enabled (for display purposes only). When the UserControl is loading, the DateTimePickers are assigned values from a DataRow that came from a DataSet which stores a single record returned from a SQL Server stored procedure.
There is an inconsistent behavior in which the users sometimes see today's date instead of the date that was assigned to the DateTimePicker. It doesn't seem to matter whether I assign the date I want to the .Value property or the .Text property:
txtstart.Value = (DateTime) dr["Group_Start_Date"];
txtend.Text = dr["Term_Date"].ToString();
I expect that of the two statements above, the one using the Value property is more appropriate. But, in both cases, today's date is displayed to the user regardless of the values that were in the database. In the case of txtstart.Value, Visual Studio shows me that the value was assigned as expected. So why isn't it displaying that date to the user instead of today's date?

I found the answer. You MUST set the checkbox value to checked to indicate a non-null value.
this.dateSold.Checked = true;
// set to true or false, as desired
this.dateSold.ShowCheckBox = false;

I was having trouble with this too and found that you indeed need to set the .Checked value to True.
I set the Checked property in the properties window and it still didn't work so I just set it in code before assigning the value and that fixed the problem.

It seems to be a problem with having multiple DateTimePickers. I was able to get around the issue (bug?) by programatically creating the DateTimePickers with the values I wanted and adding them to the form.

I ended up going with AB Nolan's suggestion. The reasoning behind a disabled DateTimePicker was never clear to me, so back on 10/23/2009, rather than continue fussing with the control I just displayed the dates I wanted in TextBoxes instead.

Related

C# DatagridView change the text entered in a cell during validation

i have a datagridView with a cell whose data type is Datetime.
The value of the cell is formatted as HH:mm (hours, minutes).
When the user edits the cell with a value like 5, I would like to transform it into 05:00 before the datagridView complains that the value entered is not a valid datetime.
I have tried the validating event but there I cannot change the Formatted value, I can only Cancel.
I know what I am asking is possible because I have done it in another project, but I cannot access the code of that project to see how I did it.
Please help. Thanks
I have solved the problem using the CellParsing event. There I get the Value entered from the user and I can parse it into a proper datetime and finally set e.ParsingApplied = true (very important otherwise the parsing you did is ignored)

comboBox C# Winform

I am having a problem. I have a comboBox in that when I am entering
BlowMolding by coding comboSub1.Text = "BlowMolding".
It is automatically picking a value from list Blowmolding.
I know it is due to the fact that values are same except the case of "M"
and "m".
But in my application it creates problem.
So, Please tell me how to stop combo from picking the value
automatically and stick to the value I am assigning to it. Even if only
the case is changed.
If I understood your problem correctly, you are trying to set some value as text of your comboBox, and you also want that the text property should be set to whatever value you set explicitly.
If this the case then you should set the DropDownStyle property of your combo box to DropDown instead of DropDownList, which prevents you from setting value apart from what is have already in list.
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
you may also set this value via prperties window.
Try this:
comboSub1.SelectedIndex = comboSub1.Items.IndexOf("BlowMolding");

Change value of DateTimePicker

I'm having problem to change the displayed value of my DateTimePicker object in C# Winforms
I'm trying to change the date in the DateTimePicker according to the value, but the display won't change.
dtpDisplayTo.Value = DateTime.Today;
I know this is dated, but I just had the same problem and found a solution with a bit of research. You have to set the Checked property of the DateTimePicker to True before changing the date, otherwise the control will consider the change as null and not show the new date. Really strange stuff.
DateTimePicker displays today's date instead of displaying its actual Value

Strange DateTimePicker Formatting Behavior

I have a small form that includes a DateTimePicker control. I've customized it so that it formats its dates to dd/MM/yy. However, when the user presses "send", its value is displayed in my CheckedListBox as dd/month/yy.
For example, if the user sets the date to "19/04/11", it shows up in the CheckedListBox as "19-Apr-2011". Does anyone have any suggestions as to why this may be happening? Thanks in advance.
change the format of the Date before passing to the Checkedlistbox by making it as
checkedListBox1.Items.Add(dateTimePicker1.Value.ToString("dd/MM/yy"));
Set the CustomFormat property of your DateTimePicker control to "dd/MM/yy"..
The Format property must be set to DateTimePickerFormat.Custom for this property to affect the formatting of the displayed date and time.
However, the Value property is not formatted unless the Checked property is set to true. If the Checked property was set to false only Text property value is formatted ..
Refer to:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.customformat.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.checked.aspx

Datagridview DateTime Format not using System Region settings

I've got a datagridview that is hooked up to a bindingsource on my main form. One of the columns and datamembers in the bindingsource is a DateTime object. When the datagridview shows this item it doesn't use Window's default region settings for displaying datetime.
The weird thing is, I can access this same DateTime object and print it to a text box, or even pass it off to another form with another datagridview and binding source and it actually shows up properly in that table.
So then, why does the datagridview on my main form not use the regional date/time settings when converting the DateTime object to a string?
I've checked that there was no special formatting applied to the datagridview and so I'm running out of ideas to check.
Do you guys know if there is any super secret way to get a datagridview to follow system regional format rules or not?
Also, since I highly doubt that I did something different with this datagridview vs the others in my application, the next potential area for issue could be on application load: Perhaps since this comes up when the application loads, it's not getting the same cultureUI settings as the other datagridviews?
Update I've tried adding another column to the datagrid view and hooking it up to the same data property (datetime) and I get the same result. So something about how entire datagridview, or how it was created is stopping the "ToString" from using the default regional settings.
Any Help?
On design, in the Edit Columns dialog of the DataGridView:
Search for property DefaultCellStyle, and open this dialog;
Search for property Format and choose Date format;
Choose your favorite format.

Categories