Mysql DATE type is displayed as DATETIME on a datagrid

Asked

Viewed 131 times

5

I create a very simple database with the following command:

CREATE TABLE IF NOT EXISTS `ABC`.`Socio` (
    `idSocio` INT NOT NULL,
    `SocioNome` VARCHAR(45) NULL,
    `SocioDataNasc` DATE NULL,
    PRIMARY KEY (`idSocio`))
    ENGINE = InnoDB;

The Inserts used in the example are:

INSERT INTO `ABC`.`Socio` (`idSocio`, `SocioNome`, `SocioDataNasc`) VALUES (1, 'Trampa', '1946-06-14');
INSERT INTO `ABC`.`Socio` (`idSocio`, `SocioNome`, `SocioDataNasc`) VALUES (2, 'Bill', '1955-10-28');
INSERT INTO `ABC`.`Socio` (`idSocio`, `SocioNome`, `SocioDataNasc`) VALUES (3, 'Cristiano', '1985-02-05');
INSERT INTO `ABC`.`Socio` (`idSocio`, `SocioNome`, `SocioDataNasc`) VALUES (4, 'Francisco', '1936-12-17');
INSERT INTO `ABC`.`Socio` (`idSocio`, `SocioNome`, `SocioDataNasc`) VALUES (5, 'Obama', '1961-08-04');

The part about the XAML that retrieves the data for the datagrid is:

            <DataGrid AutoGenerateColumns="False" Height="Auto" Width="Auto" HorizontalAlignment="Left" Name="dataGridProdutos" ItemsSource="{Binding Path=carregarDados}" CanUserResizeRows="True" AlternatingRowBackground="GhostWhite"  AlternationCount="2" CanUserAddRows="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Path=idSocio}" Header="Número de Sócio" Width="Auto" IsReadOnly="True" />
                    <DataGridTextColumn Binding="{Binding Path=SocioNome}" Header="Nome" Width="Auto" IsReadOnly="True" />
                    <DataGridTextColumn Binding="{Binding Path=SocioDataNasc}" Header="Data de Nascimento" Width="*" IsReadOnly="True" />
                </DataGrid.Columns>
            </DataGrid>

Why do I get the date in a format that appears to be of type DATETIME? inserir a descrição da imagem aqui

2 answers

6


That’s right. In C# there is no type Date, only DateTime or TimeSpan, so it is added the part of time on your date. You can solve with a StringFormat.

StringFormat=dd/MM/yyyy

Thus remaining:

<DataGridTextColumn Binding="{Binding Path=SocioDataNasc, StringFormat=dd/MM/yyyy}" Header="Data de Nascimento" Width="*" IsReadOnly="True" />

2

Use the formatting {Binding Path=SocioDataNasc, StringFormat = d}:

<DataGrid AutoGenerateColumns="False" Height="Auto" Width="Auto" HorizontalAlignment="Left" Name="dataGridProdutos" ItemsSource="{Binding Path=carregarDados}" CanUserResizeRows="True" AlternatingRowBackground="GhostWhite"  AlternationCount="2" CanUserAddRows="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=idSocio}" Header="Número de Sócio" Width="Auto" IsReadOnly="True" />
        <DataGridTextColumn Binding="{Binding Path=SocioNome}" Header="Nome" Width="Auto" IsReadOnly="True" />
        <DataGridTextColumn Binding="{Binding Path=SocioDataNasc, StringFormat = d}" Header="Data de Nascimento" Width="*" IsReadOnly="True" />
    </DataGrid.Columns>
</DataGrid>

I put in the Github for future reference.

See the documentation of the formats if you want to customize more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.