Solved

format date in text box control

Posted on 2003-11-17
5
1,105 Views
Last Modified: 2012-05-04
I have a form with an underlying dataset (from a sql server db) that is bound to various textboxes.  A couple of the fields in the dataset contain dates.  Is there a way to format the dates in the text boxes.

Currently they display as ‘06/10/2003 00:00:00’

I need them to display as 06/10/2003.

Is this possible?
0
Comment
Question by:WingYip
5 Comments
 
LVL 1

Expert Comment

by:ob2
ID: 9763177
Easy Peasy

1) TextBox.Text = String.Format("{0:d}", Date)

Where d is always lowercase for short date and where date is the date from your dataset.
0
 
LVL 3

Accepted Solution

by:
the-edge earned 50 total points
ID: 9763226
use the Binding.Format Event

search "Binding.Format Event" in msdn for further details

from msdn:
Remarks
The Format event occurs both when data is pushed from the data source into the control, and when the data is pulled from the control into the data source. When the data is pushed from the data source into the control, the Binding will use the Format event to put the formatted data into the control. When the data is pushed from the control into the data source, the Binding will first parse the data using the Parse event, then format the data and push the it into the control.

The Format and Parse events allow you to create custom formats for displaying data. For example, if the data in a table is of type Decimal, you can display the data in the local currency format by setting the Value property of the ConvertEventArgs object to the formatted value in the Format event. You must consequently unformat the displayed value in the Parse event.

The Format event occurs whenever the Current value of the BindingManagerBase changes, which includes:

The first time the property is bound.
Any time the Position changes.
Whenever the data-bound list is sorted or filtered, which is accomplished when a DataView supplies the list.
The Format event also occurs after the Parse event. For example, when a control loses focus, its contents are parsed. Immediately afterwards, as new data is pushed into the control, the Format event occurs allowing the new contents to be formatted.



the edge
0
 
LVL 3

Expert Comment

by:the-edge
ID: 9763248
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10268075
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: ob2 {http:#9763177} & the-edge {http:#9763248}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0
 
LVL 1

Author Comment

by:WingYip
ID: 10280269
Have just tested the code.

the-edge gets the points

Thanks

Wing
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 67
How to share My.Settings in multiple application instances ? 7 48
VS 2017 18 101
asp.net figure out error 3 17
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 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