Give a specific format to a date field in a gridview asp.net c #

-1

Someone knows how to format a row of gridview by codehebing

I've been trying this but can not get out:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[2].Text = e.Row.Cells[2].Text.ToString("yyyy/MM/dd");




              e.Row.Cells[1].Visible = false;
                 e.Row.Cells[3].Visible = false;
              e.Row.Cells[5].Visible = false;
              e.Row.Cells[6].Visible = false;
              e.Row.Cells[7].Visible = false;
              e.Row.Cells[8].Visible = false;
              e.Row.Cells[10].Visible = false;
              e.Row.Cells[12].Visible = false;
               e.Row.Cells[14].Visible = false;
              e.Row.Cells[15].Visible = false;
               e.Row.Cells[16].Visible = false;
                e.Row.Cells[18].Visible = false;
                e.Row.Cells[19].Visible = false;
                e.Row.Cells[20].Visible = false;
               e.Row.Cells[21].Visible = false;
               e.Row.Cells[22].Visible = false;
              e.Row.Cells[23].Visible = false;



        }

    
asked by PieroDev 14.06.2017 в 15:51
source

3 answers

0

To be able to format first what you do in converting the gridview into a LINQ.

  listadtodividendo = tabla.ConsultarTablaAmortizacion(dtorequest).ToList();
var nuevolistadtodividendo = (from o in listadtodividendo
                select new {
                    NumeroCuota = o.NumeroCuota,
                    FechaVencimiento = o.FechaVencimiento.ToString("yyyy/MM/dd"),
                      Cuota = o.Cuota,
                    AmortizacionCapital = o.Capital, o.CapitalReducido,
                    Interes = o.Interes,
                    Saldo = o.CapitalReducido //o.Capital

                }).ToList();

and so that it only forms the date that must be Year - Month - Day

ExitDate = o.DateVent.ToString ("yyyy / MM / dd"),

    
answered by 14.06.2017 / 22:29
source
1

You must have a data type DateTime to apply fomato

e.Rows[2].Text = Convert.ToDateTime(e.Rows[2].Text).ToString("yyyy/MM/dd");

To ensure the type of data, it also applies

DateTime fecha;
if(DateTime.TryParse(e.Rows[2].Text, out fecha)){
    e.Rows[2].Text = fecha.ToString("yyyy/MM/dd");
}

Although if you define the columns at design time it would be even better

Format DateTime in GridView BoundField and TemplateField columns in ASP.Net

you basically define the BoundField

DataFormatString = "{0:dd/MM/yyyy}"
    
answered by 14.06.2017 в 23:02
0

You could do the following

Datetime fecha = Convert.ToDateTime(e.Row.Cells[2]);
e.Row.Cells[2].Text = fecha.ToString("yyyy/MM/dd")
    
answered by 14.06.2017 в 15:55