Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello i have table with col date and this date has if statement to check if the date will expire in 45 show this row, if not don't row but some how this is not working can someone help or suggest new answer .

What I have tried:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Label b = e.Row.FindControl("Label27") as Label;

        if (b != null)
        {
            DateTime lblDate;
            if (!DateTime.TryParse(b.Text, out lblDate))
            {
                // date time conversion not success 
                // you may have empty or invalid datetime 
                // do something in this case 
                return;
            }

            if (lblDate <= DateTime.Today.AddDays(45))
            {

                e.Row.Visible = true;

            }

            else
            {
                e.Row.Visible = false;

            }


        }
    }
Posted
Updated 31-Mar-17 16:40pm
Comments
Graeme_Grant 31-Mar-17 22:16pm    
DateTime.Today.AddDays(45) will always be 45 days in the future from today.

There is not enough information here...

Developers sacrifice and contribute their own time for free to help fellow developers resolve difficulties. It is important that you are crystal clear about what you are experiencing with plenty of information so that your time and theirs are not wasted. The clearer the question, the better chance that you will get a favorable response in a timely manner.

Please take the time to look at these links provided before posting questions:
* Some guidelines for posting questions in the forums[^]
* Tales from the Evil Empire - Asking questions is a skill[^]

Once you are ready update the question with clear and concise details, sample code, any error messages (including inner exception details), etc, please click on Improve question to add more info to the question.
Muddassir Mohammed 31-Mar-17 22:17pm    
So the issue is the row is always visible even though the condition if (lblDate <= DateTime.Today.AddDays(45)) is true?

1 solution

try this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           Label lblDate = e.Row.FindControl("Label27") as Label;
           DateTime date; string yourDateFormat = "MM/dd/yyyy"; // check the label date format
           if (lblDate != null)
               if (!string.IsNullOrEmpty(lblDate.Text)) // check is null or empty
                   if (DateTime.TryParseExact(lblDate.Text, yourDateFormat, CultureInfo.CurrentCulture, DateTimeStyles.None, out date))
                   {
                       bool show = date <= DateTime.Today.AddDays(45);
                       e.Row.Visible = show;
                   }
       }

use DateTime.TryParseExact Method [^] to convert string to datetime.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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