Display date formatted in datagridview

Asked

Viewed 795 times

1

In the datagridview has a date field in the format yyyyMMdd and need to display formatted in the correct form.

tried this:

dgvRequisicao.Columns["data"].DefaultCellStyle.Format = "dd/MM/yyyy";

But instead of showing me the formatted date shows me dd/MM/yyyy

Edit:

I populate the grid like this:

 BindingSource sbind = new BindingSource();
                sbind.DataSource = dt;
                dgv.DataSource = sbind;

Edit:

Here I charge datatable bank

  for (int i = 0; i < parametros.Length; i += 2)
                cmd.Parameters.AddWithValue(parametros[i].ToString(), parametros[i + 1]);
            OracleDataAdapter da = new OracleDataAdapter(cmd);
            da.Fill(dt);

Thank you

  • Post the code that fills your variable dt

  • returns from an sql.. executeNonQuery

  • You can post more code to make it easier to see what you’re saying?

  • Yes... I put the for that fills the parameters and sends to the bank and the result returns in datatable.. In the datagridview I configured the columns according to the columns of select

  • The only way I could was through this datatable and put in a helper with the formatted data.. Still, thank you very much!

  • Try to take the examples I posted and adapt to your reality. Look at my Git, I put the code of what I did to try to help you.

Show 1 more comment

2 answers

2

As I observed you’re using the BindingSource, so I changed the example to use the BindingSource, being as follows

private void PessoaBindingSourceForm_Load(object sender, EventArgs e)
        {
            PessoaList list = new PessoaList();

            list.Add(new Pessoa() { Id = 1, DataHora = DateTime.Now.AddDays(1), Nome = "Pablo" });
            list.Add(new Pessoa() { Id = 2, DataHora = DateTime.Now.AddDays(2), Nome = "Pablo" });
            list.Add(new Pessoa() { Id = 3, DataHora = DateTime.Now.AddDays(3), Nome = "Pablo" });
            list.Add(new Pessoa() { Id = 4, DataHora = DateTime.Now.AddDays(4), Nome = "Pablo" });

            BindingSource sbind = new BindingSource();
            sbind.DataSource = list;
            dataGridView1.DataSource = sbind;
            dataGridView1.Refresh();
        }

public class PessoaList : BindingList<Pessoa>
    {


    }

An alternative I’ve used a lot is to add to your DataGridView one DefaultCellStyle, where in this configuration you can configure the Format.

I add some prints to make it easy

Edit Columns

CellStyle Builder

Format String Dialog

I also added to git the example

  • cool... I put formatting datetime but show me a lot of exception System.Format.Exception

  • the data type q is linking to your column, is Datetime?

  • I’m populating a datatable

  • I’ll make an example here to see how it looks

  • I edited the answer by adding the example

  • If you can, edit your question and add the code that populates Datagridview

  • changed the question

Show 2 more comments

0

do not do this kind of manipulation by datagridview, bring the formatted date straight from the database,

SELECT DATE_FORMAT(seuCampoDatanaTabela,'%d/%m/%Y') AS AliasSeQuiserUsar FROM suaTabela

Browser other questions tagged

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