0

I have a variable that receives DATETIME type ,And sometimes comes from DB NULL variable So I have this code .

DateTime? d;
DateTime dtq; 
don.Date_appeal_donor= d = DateTime.TryParse(dr["Date_"].ToString(), out dtq) ? dtq : (DateTime?)null;

And I can not change the format to "dd / MM / yyyy" Does anyone have a solution?

You're right, now I realized that only when it comes to DATAGRIDVIEW It changes the display This is in wpf. So I fill the data grid

  dataGrid1.ItemsSource= DAL.LoadCollectionData();

And here I am stuck

1
  • 4
    I assume that dr["Date_"] actually already is a DateTime variable, why do you convert it then first to a String and then try-parse it back to a DateTime? Commented Jun 25, 2012 at 10:24

1 Answer 1

1

You can use DateTime.TryParseExact like this:

DateTime dtq;
DateTime? d;

don.Date_appeal_donor = d = DateTime.TryParseExact(dr["Date_"].ToString(), "dd/MM/yyyy", null, DateTimeStyles.None, out date) ? dtq : (DateTime?)null;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.