Solved

How to Parse a Date in RowDataBound Event

Posted on 2014-01-22
3
462 Views
Last Modified: 2014-01-27
Hello, Experts!  I am using ASP.NET with C#.  
My gridview is built dynamically based on whatever table is being used.  I have Date, DateTime, and Time fields throughout my tables.  In the GrdView, my Dates are showing 12:00:00 for time where there should be no time.  I am trying to parse out the Date, but am stuck.  How do I set up the parse?   Here is my code:

protected void grdvwReview_RowDataBound(object sender, GridViewRowEventArgs e)
 {
         
            if (e.Row.RowType == DataControlRowType.DataRow) 
            {
                for (int i = 1; i < e.Row.Cells.Count; i++)
                {
                    BoundField field = (BoundField)((DataControlFieldCell)e.Row.Cells[i]).ContainingField;

                    if (field.HeaderText.Contains("Date"))
                    {
                        string l_type = "";
                        string strt = "";
                        l_type = CheckDataType(field.ToString());
                        if (l_type == "Date")
                        {
                            strt = DateTime.Parse(e.Row.Cells[i].Substring(0, 10).ToString( 'dd/mm/yyyy'));
                            // display the correct format
                        }
                   }
               }
            }
}

Open in new window

0
Comment
Question by:ptslv
  • 2
3 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39801309
Shouldn't it be:
strt = DateTime.Parse(e.Row.Cells[i].Substring(0, 10).ToString(), "dd/mm/yyyy");

Open in new window

or
strt = DateTime.Parse(e.Row.Cells[i].Substring(0, 10), "dd/mm/yyyy");

Open in new window

?

HTH,
Dan
0
 

Accepted Solution

by:
ptslv earned 0 total points
ID: 39801405
Dan,

I could not get the SubString to work.  I was able to fix it though.  I used:

strt = DateTime.Parse(e.Row.Cells[i].Text.Trim()).ToShortDateString();
                                
                                e.Row.Cells[i].Text = strt.ToString();

Open in new window

0
 

Author Closing Comment

by:ptslv
ID: 39811516
I answered my own question.  Using SubString()would not work.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Web Data Entry Application 5 90
Web Development Tools for MVC 3 91
Question to locate the problem 18 108
502 - Web server received an invalid response 4 88
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

777 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