Force date format in datagridview

1

Good afternoon: I have a Dgv with a cell that contains the Date, In the Load of the Form I do the following:

dgvContratoRenov.Columns[14].DefaultCellStyle.Format = "d";

When executing the Project, the Date appears well (Ej: 05/04/2017) . My problem is that at the time I modify it I do not respect the "/" and I have to write the full date. The idea is that it works as if it were a MaskedTextbox , that is, que después de ingresar el día se salte a la posición del mes y luego del año.

Is it possible? Thanks

    
asked by Mario Escudero 15.04.2017 в 00:46
source

3 answers

1

I've tried with the library and from the same designer of the winform you can select the new one type DataGridViewMaskedTextColumn

In the Designer.cs file it would look like this:

//Declaración
private JThomas.Controls.DataGridViewMaskedTextColumn Fecha; //Declaración de la columna

...

private void InitializeComponent()
{
  //...
  this.Fecha = new JThomas.Controls.DataGridViewMaskedTextColumn();

  //...
  this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Fecha});

   // 
   // Fecha
   // 
   dataGridViewCellStyle1.Format = "d";
   this.Fecha.DefaultCellStyle = dataGridViewCellStyle1;
   this.Fecha.HeaderText = "Fecha";
   this.Fecha.Mask = "00/00/0000";
   this.Fecha.Name = "Fecha";
   this.Fecha.Resizable = System.Windows.Forms.DataGridViewTriState.True;

 //...
}

I do not know if it's late but I hope it can be useful to someone. Greetings

    
answered by 29.08.2018 в 10:21
0

Hello friend I think this can help you put this code:

 Convert.ToDateTime(DtSet.Tables[0].Rows[0]["FInicioEmpresa"].ToString()).ToString("yyyy-MM-dd");
    
answered by 15.04.2017 в 02:06
0

Use

dgvContratoRenov.Columns[14].DefaultCellStyle.Format = "dd/MM/yyyy";

Either when you initialize your DataGridView or through the designer.

    
answered by 05.02.2018 в 01:14