Solved

How to Parse a Date in RowDataBound Event

Posted on 2014-01-22
3
440 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
Comment Utility
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
Comment Utility
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
Comment Utility
I answered my own question.  Using SubString()would not work.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Define alias for servlet 5 41
Dynamically highly text on a webpage 9 97
PHP Curl Setup Option 4 28
Re-imbursement Claim System 3 21
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
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…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

771 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

11 Experts available now in Live!

Get 1:1 Help Now