3

I had a TimeSpan saved into SqlCe in ticks, and when i load the data in a DataGrid i want to format this value in HH:MM:SS. I try it with this:

<DataGridTextColumn Binding="{Binding tiempo, StringFormat={}{0:hh':'mm':'ss}, TargetNullValue=' --- '}"  Width="80" Header="Tiempo"/>

But the DataGrid shows 'hh:mm:ss' instead of the value.

i try also try it with other patterns like StringFormat="hh\:mm\:ss"

Any idea?

Thanks! and sorry for my bad english!

Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
Roomm
  • 905
  • 11
  • 23
  • Not sure if you can use `String.Format` inside your binding. But you can easily achieve this by using a `ValueConverter`. Take a look at this [tutorial](http://wpftutorial.net/ValueConverters.html) – Arian Motamedi May 11 '13 at 18:10
  • @programmer93 That's the [StringFormat](http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.stringformat.aspx) property, not the String.Format method. – Clemens May 11 '13 at 19:17

2 Answers2

5

You could write it like this with double backslashes:

<DataGridTextColumn Binding="{Binding tiempo, StringFormat=hh\\:mm\\:ss}"/>

or like this with single backslashes:

<DataGridTextColumn>
    <DataGridTextColumn.Binding>
        <Binding Path="tiempo" StringFormat="hh\:mm\:ss"/>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

although that is already the default formatting, so

<DataGridTextColumn Binding="{Binding tiempo}"/>

should also be ok.

See also this answer for a few more examples.

Community
  • 1
  • 1
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • mm the same result, the datagrid shows hh:mm:ss but not the formatted value – Roomm May 15 '13 at 06:42
  • I even tried it with a DataGridTextColumn and it works for me. Could you show the declaration of the `tiempo` property? – Clemens May 15 '13 at 06:54
0

i solved the problem! in database i was storing "tiempo" like bigint, so i changed it to nvarchar and making a few fixes it works. thanks for all!

Roomm
  • 905
  • 11
  • 23