Solved

How to Parse a Date in RowDataBound Event

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! https://www.google.com/about/careers/students/guide-to-technical-development.html How to Know You are Making a Difference at EE In August, 2013, one …
Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
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 integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

830 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