473,385 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Check if string has valid DateTime format

Hi,

I have the following event which checks if a cell in a dgv contains a
date and if so, checks if it is formatted correctly. I don't think its
good practice for be to try this by catching an exception. Is there a
better way? Notice also my use of a dummy dateTime object. Also, does
the DateTime object contain any information about how it should be
formatted? For example -

"Format Error: DateTime cell incorrectly formatted. Expected format: "
+ DateTime.ExpectedFormat;

Thanks,

Barry.

private void DataGridView_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
DataGridView dataGridView = ((DataGridView)sender);
DataGridViewCell dataGridViewCell =
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];

// if formatting a DateTime cell
if (dataGridViewCell.ValueType == typeof(DateTime))
{
try
{
DateTime dateTime = (DateTime)((new
DateTimeConverter()).ConvertFromString((string)e.F ormattedValue));
}
catch (FormatException formatException)
{
dataGridView.Rows[e.RowIndex].ErrorText = "Format
Error: DateTime cell incorrectly formatted.";
e.Cancel = true;
}
}
}
Oct 1 '08 #1
1 15245
<Ma*************@gmail.comwrote in message
news:11**********************************@75g2000h so.googlegroups.com...
[...] I don't think its
good practice for be to try this by catching an exception. Is there a
better way?
You can use DateTime.TryParse (or TryParseExact), which will return
false if the string cannot be parsed as a DateTime, without throwing an
exception.

Oct 1 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Dan S | last post by:
My application asks the user to enter in a date - in the mm/dd/yyyy format. Is there any quick and easy way to verify the date the user enters is formatted correctly? Right now I'm calling...
2
by: Sterling Ledet | last post by:
I am trying to create a web service that takes a string from my web server in the following format: Mon, 6 Oct 2003 18:39:47 UTC and put's in a datetime so it can then be reformatted in C# as...
4
by: Hans Merkl | last post by:
Does anybody know of a library that can handle strings pf various formats and conver them to a DateTime value? The strings are coming from a webform and I can't restrict the input (it's not my...
38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
50
by: z. f. | last post by:
HI, i have string in format dd/mm/yyyyy hh:mm:ss and giving this as an input to DateTime.Parse gives a string was not recognized as a valid date time format string error. how do i make the parse...
10
by: Kiran | last post by:
Hi, I am trying to convert a string(Thu Jul 21 00:00:00 UTC+0100 200) to date. I get the following error Cast from string "Thu Jul 21 00:00:00 UTC+0100 200" to type 'Date' is not...
1
by: Thomas | last post by:
Hi all, i have to check if a value is a valid date format depending on a "format string" the user specified. For example: Format String: "dd/mm/yyyy" Date Value: "01/12/2006" Valid: Yes! ...
7
by: tolisss | last post by:
Hi i have a string like "22/5/2006 12:00:00 pµ" and i want to create a string like "05/22/06" could someone please
2
by: Brian Parker | last post by:
I am beginning to work with VB2005.NET and I'm getting some problems with string formatting converting an application from VB6. VB6 code:- sTradeDate = Format(pArray(4,i Record), "mmddyy") ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.