Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7178095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:51:03+00:00 2026-05-28T16:51:03+00:00

In my application I have a datagrid that populates with info from a SQLserver

  • 0

In my application I have a datagrid that populates with info from a SQLserver DB. Amongst other things the data which populates the grid includes times. Now I struggled for some time getting the data from the DB to look the way I wanted. In the end my sql comman included the following : –

LEFT(CONVERT(VARCHAR(8), te.StartTime, 8), 5) AS [Start Time], 
LEFT(CONVERT(VARCHAR(8), te.FinishTime, 8), 5) AS [Finish Time]

Which nicely shows times in the format of 8:00 & 10:00 etc.
However, this causes a problem later on in my application when I need to use the info in the datagrid to calculate the difference between two times. This is because (I believe) my SQL command (above) has coverted the times to strings. So when I try the following for example:

DateTime startTime = (DateTime)varValue;
DateTime endTime = (DateTime)varFinish; 

I get the error message ‘invalid cast’. So I’m faced with converting these strings back into DateTime in order to perform any calculations with them. I can’t simply add the original time fields to the SQL command, because sometimes the times in the grid will be new times input by the user, so those will be strings anyway.

How should I best convert the user input e.g. “8:00” into a datetime – so that I can perform functions like calucating the difference between two times. Or alternatively, should I be going about the whole timesheet issue a different way.

Any advice will be greatly appreciated. I’m rather new to C# as you may have guessed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:51:05+00:00Added an answer on May 28, 2026 at 4:51 pm

    Have you tried this?

    DateTime tmp = DateTime.Now;
    DateTime startTime = DateTime.Parse(varValue + " " + tmp.ToShortDateString());
    DateTime endTime = DateTime.Parse(varFinish + " " + tmp.ToShortDateString()); 
    

    Think this could solve your Problem?
    You got now the current Date with your given times in the two parameters

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I have a TcxGrid (devexpress datagrid) that displays the data returned
So when i have a Datagrid which i want to fill with data from
I have a Silverlight application that is using a DataGrid. Inside of that DataGrid
In a WinForms application, I have a datagrid that is associated to a datasource.
I am making a WPF-application.I have a datagrid with a column header that contains
In my application i have a WPF Window that has a DataGrid in it.
My main form in my application has a datagrid view that can have 1
I am working in a Silverlight 4 application. I have a datagrid that I
i am porting my application from windows forms to WPF. I have found that
I have a DataGrid in a WPF application that binds itself to an ObservableCollection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.