Click here to Skip to main content
15,889,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have been going through the motions and still cannot seem to get this right. I have text boxes in a grid view that are populated from an access database. They are all date time values. In the back-end code, I am trying to loop through all those values and then apply conditional formatting. For some unknown reason I am unable to get the value from those text boxes in the grid view and when I do, they are seen by the application as string as opposed to date time. Converting is futile as the same error, "String was not recognized as a valid DateTime." keeps popping up.

Any ideas on how to get values from a grid view text box, and then convert them from a string to a date time format?

Here is the code thus far.
C#
var myLabel2 = (TextBox)GridView1.Rows[p].Cells[0].FindControl("Label2");
            var myLabel4 = (TextBox)GridView1.Rows[p].Cells[0].FindControl("Label4");

            var myLabel7 = (Label)GridView1.Rows[p].Cells[0].FindControl("Label7");
            var myLabel9 = (Label)GridView1.Rows[p].Cells[0].FindControl("Label9");

            string dateString = myLabel2.Text;

            CultureInfo provider = CultureInfo.InvariantCulture;
            string format = "dd/MMM/yyyy h:mm tt zzz";
            DateTime result = DateTime.ParseExact(dateString, format, provider);

            DateTime start = Convert.ToDateTime(myLabel2.Text).Date;
            DateTime now = DateTime.Now.Date;
            DateTime end = Convert.ToDateTime(myLabel4.Text).Date;

            if (now >= start && now <= end)
            {
                myLabel2.BackColor = Color.Chartreuse;
                myLabel4.BackColor = Color.Chartreuse;
                myLabel7.BackColor = Color.Chartreuse;
                myLabel9.BackColor = Color.Chartreuse;
            }
            else
            {
                myLabel2.BackColor = Color.White;
                myLabel4.BackColor = Color.White;
                myLabel7.BackColor = Color.White;
                myLabel9.BackColor = Color.White;
            }

Thanks in advance
Posted
Updated 8-Apr-12 6:40am
v3
Comments
Reza Ahmadi 8-Apr-12 11:04am    
I could not understand yet, could you get the value of the Text property in your code behind? and if so, have you debugged to realize what is exactly in your labels' Text property?
[no name] 8-Apr-12 11:14am    
Have you debugged? Are you sure of the value in your textboxes? The error message is very clear.
VirgilPlatoKeats 8-Apr-12 12:31pm    
The value of the textboxes are there, but the format which it perceives is string. Is there a simple method to get a value from a gridview textbox and then convert it from string to datetime?
Reza Ahmadi 8-Apr-12 12:33pm    
DateTime.Parse can be used to convert a date string to date.
VirgilPlatoKeats 8-Apr-12 12:39pm    
I have tried various forms of Parse to convert, yet the same error keeps popping up...

Check your language settings. The standard English settings are mm/dd/yyyy and you trying to convert a date in the format yyyy/mm/dd.
 
Share this answer
 
v2
Comments
VirgilPlatoKeats 9-Apr-12 11:59am    
DateTime date = DateTime.ParseExact(myLabel2.Text, "mm/dd/yyyy", null);

Would this work to convert it? It still gives the same error? Is there an alternative method to Parse?
Use Convert.ToDateTime("SomeDateString")

string dateString = "2012/04/03 12:00:00 AM";
DateTime _dt = Convert.ToDateTime(dateString);
 
Share this answer
 
Comments
VirgilPlatoKeats 8-Apr-12 13:54pm    
Still no solution, "String was not recognized as a valid DateTime."... The problem is getting the value from the label that is loaded into the gridview... I have tried the following...

string dateString = myLabel2.Text;
DateTime _dt = Convert.ToDateTime(dateString);
Ashish Bhujbal 9-Apr-12 8:13am    
1> Before using your above code, make sure that myLabel2.Text in not a empty string.
2> if both label giving you proper values then please revert back with your date format, which is at your labels.
I'm not sure what your problem is. Try this, it works:

DateTime t1 = DateTime.Now.AddDays(1).Date;
    DateTime t2 = DateTime.Now.Date;
    DateTime t3 = DateTime.Now.AddDays(-1).Date;
    if (t1 > t2 && t2 > t3)
    {
        Console.WriteLine("It worked");
    }


I would think you did not check the dates to see if they were in the format you expected. Could be you were assuming some other format.
 
Share this answer
 
Comments
VirgilPlatoKeats 8-Apr-12 15:59pm    
This situation is fine if you are not working with values from labels within a gridview. This case, it works, but when you combine conditional formatting with a conversion of data from a string to datetime, certain errors arise.

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