简体   繁体   中英

C# converts T-SQL date datatype to DateTime datatype by default

When transfering a table from a database to a datagrid (WPF), the DATE datatype from T-SQL is converted to DateTime, but I don't want that. I want only the .Date part to show up in my datagrid.

Here's the piece of code in c#:

cmd = new SqlCommand("SELECT * FROM bibliotvguia.get_progtv();", con);

adap = new SqlDataAdapter(cmd);

dt = new DataTable();
adap.Fill(dt);
programs_datagrid.ItemsSource = dt.DefaultView;

And the XAML piece of code:

<DataGrid SelectionMode="Single" Name="programs_datagrid" Height="340"
                      AutoGenerateColumns="False" RowHeaderWidth="0" IsReadOnly="True" CanUserResizeColumns="True"
                      CanUserReorderColumns="False">
                    <DataGrid.Columns >
                        <DataGridTextColumn Header="Canal de TV" Binding="{Binding canal_media}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Dia" Binding="{Binding dia_media}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Hora" Binding="{Binding hora_media}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Produção Audiovisual" Binding="{Binding titulo}"></DataGridTextColumn>
                    </DataGrid.Columns>
                </DataGrid>

The dia_media column is the one that has a DateTime type.

Thanks for your help.

The best for you.

There is only the DateTime type in NET, so you can't really have a variable with only the Date part. This kind of problem should not be resolved changing the storage of your datetime values or inserting complicate SQL function to extract the value as string or to remove the time part.

It is only just a matter on how do you format your data for presentation.

I suppose that you need something like this in your XAML

  <DataGridTextColumn Header="Dia" Binding="{Binding dia_media, 
                                 StringFormat=dd MM yyyy}"/>

You can use a converter in your binding

XAML

       <Window.Resources>
         <locl:DateTimeConverter x:Key="converter"/>
       </Window.Resources>
     ...
        <DataGrid ItemsSource="{Binding Dates}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Date" 
                                    Binding="{ Binding DateProperty,
                                               Converter={ StaticResource converter}}"/>
            </DataGrid.Columns>
        </DataGrid>

The converter class:

 public class DateTimeConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime dateValue = (DateTime)value;

            return dateValue.ToString("yyyy/MM/dd/");
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

and the result :

在此输入图像描述

I hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM