Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have Date fields in DateGridView. On a button click, i will select the particular column to find the FUTURE and INVALID (Ex: 00/11/2000, 11/00/2001, 11/12/0000).

And blank all those. Can you please let me know the best and short process?
Posted
Comments
CHill60 22-Dec-15 6:01am    
What have you tried?

1 solution

The process is to go through the rows one by one and check the value in that column

You can use TryParse[^] or TryParseExact[^] to check whether the date is valid or not. For dates I recommend the latter (TryParseExact) so that Culture and format does not cause issues.

C#
const int colWithDate = 0;

for (var i = 0; i < GridView1.Rows.Count; i++)
{
    if (GridView1.Rows[i].Cells[0].Value != null)
    {
        DateTime checkDate;
        if (!DateTime.TryParseExact(GridView1.Rows[i].Cells[colWithDate].Value.ToString(), "d/m/yyyy", new CultureInfo("en-GB"),DateTimeStyles.None,  out checkDate))
            GridView1.Rows[i].Cells[0].Value = "";
    }
}
 
Share this answer
 
Comments
BillWoodruff 22-Dec-15 10:30am    
+5
Member 8010354 24-Dec-15 4:53am    
What's this CultureInfo and DateTimeStyles?
CHill60 24-Dec-15 5:21am    
If you follow the link in the solution it will take you to the MSDN documentation that will explain all.
Dates are culture specific - e.g. In the UK 10/5/2015 = 10th May 2015 but in the US that same text represents 5th October 2015. I have specifically said that I want to use UK date formats. DateTimeStyle is another way of determining the format for the string representing the date.
As I said, follow the link to find the full documentation

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900