Display date formatted in datagridview

1

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

I tried this:

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

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

Edit:

Populate the grid like this:

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

Edit:

Here I load the datatable of the 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

    
asked by anonymous 05.07.2016 / 16:07

1 answer

2

As you can see, you're using BindingSource , so I've changed the example to use 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 that I used a lot is to add a DataGridView in your column of DefaultCellStyle , where in this configuration you can set Format .

Add some prints to facilitate

I'vealsoaddedgitthe example

    
05.07.2016 / 16:30