Format cell in DataGrid at runtime C #

1

I'm creating a DataGridView automatically with a time column and I'm using the 00:00 time format, as follows.

dgvhorario.ColumnCount = 1;
dgvhorario.Columns[0].Name = "Horário";
dgvhorario.Columns[0].DefaultCellStyle.Format = "t";
dgvhorario.Columns[0].Width = 80;

But in execution, the time mask is not appearing: 00:00

Does anyone know how to fix this?

    
asked by anonymous 15.08.2014 / 16:33

1 answer

1

Try this:

dataGridViewCellStyle.Format = "hh:mm"; //mudei para facilitar o entendimento
this.date.DefaultCellStyle = dataGridViewCellStyle;

I made a small example here:

using System;
using System.Data;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  public class Form1 : Form
  {
    DataGridView dgv = new DataGridView();
    public Form1()
    {
      this.Controls.Add(dgv);
      DataTable dt = new DataTable();
      dt.Columns.Add("Current Time", typeof(DateTime));
      dt.Rows.Add(new object[] { DateTime.Now });
      dgv.DataSource = dt;
      dgv.Columns[0].DefaultCellStyle.Format = "hh:mm";
    }
  }
}
    
15.08.2014 / 16:46