Format cell in Datagrid at runtime C#

Asked

Viewed 1,783 times

1

I am creating a Datagridview automatically with a time column and am using the time format 00:00, as follows below.

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

But at the execution, there is appearing the mask of hours: 00:00

Can someone tell me how to fix this?

1 answer

1


Try using 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";
    }
  }
}
  • Your code did not work jpklzm friend. There is how to explain better

  • What kind of data do you use to pass the value of the cell? Take the time of a datetime or go through string?

  • In this way that you are presented I can not at the time of execution, that is when I start the form and I will write something in the cell it does not format automatically

Browser other questions tagged

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