Controlling date formats in DataGridView

Posted on 2007-09-28
Last Modified: 2013-11-07
In a DataGridView column, I have set the cell template date format style to "yyyy/MM/dd", but as soon as I click on the new line, that date format reverts to the system short date format ("dd/MM/yyy").  Here is my code:

_notesGrid.Columns["NotesDate"].CellTemplate.Style.Format = "yyyy/MM/dd";

What extra code must I add to avoid this and get all the dates appearing as "yyyy/MM/dd"?
Question by:corebiz
    LVL 14

    Expert Comment

    if the grid is binded to a data source...
    better way is to write the formatting in the that you dont need to set your style in the grid...
    LVL 14

    Expert Comment

    hope this helps...

    To do what you want to do, simply make a handler for the ItemDataBound event for the data grid row, and use a String.Format(fmt,arg) call to impose your desired formating. As it's read only, you don't need to create your own parse override routine for a custom text edit control.
    LVL 2

    Expert Comment

     check this link if this helps you..


    Accepted Solution

    Thanks for your input.

    After running around in circles for another 3 hours, I discovered the DefaultValuesNeeded event and used that to assign the value in exactly the format I wanted.

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now