Solved

How to Parse a Date in RowDataBound Event

Posted on 2014-01-22
3
452 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What do you call the following portion of a Web Application 12 72
Google Maps 7 72
Intranet building Software 8 95
Dreamweaver server behavior gone 6 108
If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now