views:

38

answers:

2

I have to format a string ("00:10:08:10") into (10 weeks 08 days 10 hrs). So I used a converter and added it with the datagrid binding. My code:

  Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
    Dim strList(4) As String
    Dim prefixList() As String = {"Wk", "Dy", "Hrs", "Min"}
    Dim input As String = value.ToString
    Dim Output As String = String.Empty
    If input <> String.Empty Then
        strList = input.Split(":")
        Dim itr As Integer
        For Each Str As String In strList
            If Integer.Parse(Str) <> 0 Then
                Output += Str + " " + prefixList(itr) + " "
            End If
            itr += 1
        Next
    End If
    Return Output
End Function

I am getting it converted but when I edit the data, the converter is called again where I wouldn't have the old format("00:10:08:10") and instead its (10 Dy 08 Hr 10 Min). so the converter fails obviously. How can I make it work?

Additional: And is there a data type in mssql to represent my data in days and hours so that i wont have to use this string format.

+1  A: 

About the additional: I always use timestamps (milliseconds since 1/1/1970) when storing times in sql. They are very easy to sort and can be parsed to and from DateTime object (or similar) in almost every programming language.

Martin
But how 'll that suit my requirement? I have to represent the deadline for a particular task. so how can I use a timestamp to reprenst 10 weeks/ 3 days ?
Sarath
@Martin - didn't you mean a timespan?
geoff
If your deadline will be static (e.g. meaning a particular due date), you can store that date as a timestamp. Then convert it back into a DateTime and subtract DateTime.Now() from it. This gives you the remaining time. You can also convert DateTime.Now() to timestamp and subtract it from the due date timestamp to get the difference in milliseconds.
Martin
@geoff - timespan would be the difference between two dates. The term 'timestamp' is from the MySQL shore.
Martin
Martin: thanks :)
Sarath
+1  A: 

You need to implement the ConvertBack function in your ValueConverter to convert from 10 Dy 08 Hr 10 Min to 00:10:08:10.

Assuming the string is in the correct format, it shouldn't be too difficult to parse and convert.

You might consider using a timespan to hold the data. It stores a length of time or duration.

geoff