How to Parse a Date in RowDataBound Event

Posted on 2014-01-22
Medium Priority
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

Question by:ptslv
  • 2
LVL 35

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

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

Open in new window



Accepted Solution

ptslv earned 0 total points
ID: 39801405

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


Author Closing Comment

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

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

587 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