1

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.

5
  • Instead of select *, select all the fields but the date one. For that, use the convert function to put it into a string in the format you want. Commented May 31, 2014 at 12:58
  • DATE column type is for datetimes. Why don't you want this? If you want to show only .Date property, then you can bound their .Date properties to your datagrid. Of you can use "d" standart pattern. What is bibliotvguia.get_progtv() for exactly by the way? Commented May 31, 2014 at 12:59
  • 1
    Given the time part is going to be zero, why do you care? Having it there is far less of an issue than storing dates as strings which is a baaaaaaaaaaaaad idea anytime. Commented May 31, 2014 at 12:59
  • You must show your XAML for better assistance. Commented May 31, 2014 at 13:00
  • Soner, the get_progtv() is an UDF to extract a table from my schema Commented May 31, 2014 at 13:03

2 Answers 2

4

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}"/>
Sign up to request clarification or add additional context in comments.

2 Comments

It worked but there's some aditional characters ()dd-mm-yyyy
Just change string format to dd-MM-yyyy then. You didn't specify the output format, so Steve's guess is as good as any.
-1

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 :

enter image description here

I hope it helps

3 Comments

Wasn't me who marked you down, but converting dates to strings is the last thing you do to them. Any chance you might want to convert them back to a date is a potential bugfest.
I thought he just wanted to present the date in that format. Steve's answer is better for the scenario you describe. What I wanted to do is just offer an alternative means to customize the way data can be presented using converters
I didn't figure you were going out of your way to lead the OP wrong mate, but have a think on this. Once you convert a date to a string, "This is not a date" is a valid entry in the "Date" column in the grid...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.