Wednesday, July 3, 2013

Properly Formatting Dates When DataBinding to a MaskedTextBox Control

Sometimes using MaskedTextBox control in a Windows Form does not display correctly the date when you bind it with a DateTime data type. It removes the leading ZEROES of the date when you run the program. Like this example below:


The solution is before databinding the MaskedTextBox control to the DataSet at runtime, you should apply the necessary format string you want your date to be displayed. Check out the code below for the solution (txtDateHired as the name of the MaskedTextBox Control):

Binding dateHired = new Binding("Text", ds, "Employees.DateHired");
dateHired.FormattingEnabled = true;
dateHired.FormatString = "MM/dd/yyyy";
txtDateHired.DataBindings.Add(dateHired);

Using this code, the dates will display properly on the form when using a MaskedTextBox control as part of your User Interface.

6 comments:

  1. hi VP
    i need database for your code at PSC for project named "Production Program (Complete Application in VB.Net & C# Code)". if you pl. give me the download link or send it to my email at prj.navin@gmail.com

    Thanks

    ReplyDelete
  2. Hello VPDJ. Greetings from Venezuela.
    I saw your production system and it seemed interesting, but I need databases for learning and testing purposes. I will appreciate if you send me your mail to my oaguiar@gmail.com.
    I wish you all the luck, and thanks

    ReplyDelete
  3. Thanks a lot for the good articles. I also saw your production system and it is very interesting, can you please provide the databases for learning and testing purposes. My email is: emulondo@nkumbauniversity.ac.ug
    I wish you all the luck, and thanks

    ReplyDelete
  4. Hi VP,
    Kindly share with me you Production Program Database.My email abdazz25@gmail.com

    Thanks and regards
    Abdalla

    ReplyDelete
  5. Hi VP,

    I saw your production system program on psc, it seems interesting to me, would you please send the database to my email, ruslan.graha@yahoo.com, thx in advance


    Sincerely,
    Ruslan

    ReplyDelete
  6. Hi VP,
    I saw your production program on PSC. May have the database for testing.
    thank you

    ReplyDelete