11

I use a WPF Toolkit Datagrid with a LINQ to SQL

       <my:DataGrid AutoGenerateColumns="False" Name="dataGrid2">
        <my:DataGrid.Columns>
            <my:DataGridTextColumn Header="Date" MinWidth="80"
                                       Binding="{Binding Date, StringFormat=d}"
                                       CanUserSort="False"/>
            <my:DataGridTextColumn Header="Time"  MinWidth="70" 
                                       Binding="{Binding Time}"
                                       CanUserSort="False" />
            <my:DataGridTextColumn Header="Description" MinWidth="200"
                                       Binding="{Binding Description}" 
                                       CanUserSort="False"/>
        </my:DataGrid.Columns>
    </my:DataGrid>

Column Time is bound to a SQL Server table field of a Time datatype. Now time value on the Datagrid is displayed in a format hh:mm:ss.

How could I change a time represantation in the Time column of a Datagrid to hh:mm, removing seconds?

EDIT: Using StringFormat=t gives no result.

<my:DataGridTextColumn Header="Time"  MinWidth="70" 
                                       Binding="{Binding Time, StringFormat=t}"
                                       CanUserSort="False" />
rem
  • 16,745
  • 37
  • 112
  • 180

4 Answers4

9
<toolkit:DataGridTextColumn
                    IsReadOnly="True"
                    Width="SizeToCells"
                    Header="F. Resolución"
                    Binding="{Binding ColName, StringFormat=MM/dd/yyyy}" />
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
4

without seconds

<toolkit:DataGridTextColumn
                    IsReadOnly="True"
                    Width="SizeToCells"
                    Header="F. Resolución"
                    Binding="{Binding ColName, StringFormat='MM/dd/yyyy HH:mm'}" />

with seconds

<toolkit:DataGridTextColumn
                    IsReadOnly="True"
                    Width="SizeToCells"
                    Header="F. Resolución"
                    Binding="{Binding ColName, StringFormat='MM/dd/yyyy HH:mm:ss'}" />

here is only time with seconds:

<toolkit:DataGridTextColumn
                    IsReadOnly="True"
                    Width="SizeToCells"
                    Header="F. Resolución"
                    Binding="{Binding ColName, StringFormat='HH:mm:ss'}" />
Farzad J
  • 1,089
  • 2
  • 14
  • 28
  • another solution would be to convert your Timespan ("timespan2 in C# is equal to the "time" dataType in sql server) into DateTime and then try the above solution ! – Farzad J Feb 20 '14 at 12:11
  • it worked for me, but how can I make ...StringFormat='HH:mm:ss'}" not present in hundred hours? – Kokombads Feb 13 '15 at 09:23
2

Can be done using ValueConverter:

This can help: Built-in WPF IValueConverters

Community
  • 1
  • 1
Shoaib Shaikh
  • 4,565
  • 1
  • 27
  • 35
0

You will need to convert your type from SqlDateTime to DateTime in order for the string format to work. Do this by casting your parameter to datetime. So for example when using c# to bring in your table.

dlEntry.StartTime = (DateTime)reader.GetSqlDateTime(3);

This will give you the ability to use the C# default converter for time.

<my:DataGridTextColumn Header="Time"  MinWidth="70" 
Binding="{Binding Time, StringFormat=t}"
CanUserSort="False" />
C Ried
  • 290
  • 4
  • 15