C# problems loading Data Grid using Datetime

Asked

Viewed 220 times

2

Good night. I have one dataGrid which I carry using Entity Framework, however in dataGrid my date appears MM/dd/yyyy, I want to remove the time and format the date to dd/mm/yyyy, and in the validity field I want to remove only the hour part of the code that generates the dataGrid

Code

using (ConsultorioContext ctx = new ConsultorioContext())
 {
     var lista = ctx.Produtos.ToList();
     return lista;
 }

Datagrid

Imagem da tela/ grid

I tried to change the bank but I still have the same problem.

1 answer

2


Change the character of the project Defaltcellstyle.Format to the Custom time and date formatting string "dd/MM/yyyy".

Supposing that your DataGridView be called dataGrid and the time/date column index where you want to apply the formatting 3, the code would be that:

dataGrid.Columns[3].DefaultCellStyle.Format = "dd/MM/yyyy";

EDIT:

As informed that the object is a DataGrid and not DataGridView then the code is:

(dataGrid.Columns[3] as DataGridTextColumn).Binding.StringFormat = "0:dd/MM/yyyy";

or in XAML inside the 'Binding' attribute of the column displaying the date, if it is present, add StringFormat='0:dd/MM/yyyy'.

<DataGrid.Columns>
   <DataGridTextColumn Binding="{StringFormat='0:dd/MM/yyyy'}" />
</DataGrid.Columns>
  • 1

    Good afternoon I could not apply, should I touch some part of the code besides this? dgProdutos.Columns[4].DefaultCellStyle.Format = "dd/MM/yyyy" me is returning the Datagridcolumn error does not contain a definition for "Defaultcellstyle" that accepts the first argument of type "Datagridcolumn"

  • dgProdutos is not a 'Datagridview? Pergunto porque na pergunta está marcado a tag Datagridviewe essa resposta só se aplica aDatagridview. Se esse é o caso me informe a classe que dgProducts` instance.

  • this is the code in the xaml <DataGrid x:Name="dgProdutos" Margin="78,172,70,243" IsReadOnly="True" MouseDoubleClick="DgProdutos_MouseDoubleClick" SelectionMode="Single" /> Part of datagrid mount code dgProdutos.ItemsSource = ProdutoViewModel.ExibirProdutos(); Remainder of the call using (ConsultorioContext ctx = new ConsultorioContext()) &#xA; { &#xA; var lista = ctx.Produtos.ToList(); &#xA; return lista; &#xA; }

  • Vasques, you were correct I am working with datagrid and not dataGridView, I did not know the difference between them.

  • @Lucassantos I made the modification to Datagrid.

  • Perfect, thank you very much, really help me a lot

Show 1 more comment

Browser other questions tagged

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