1

I'm new in c# , but i can do the basics. i need to change all the values of a column and then update the datagrid. The values are in 20170202 format and i want them like 2017-02-02. the method i did works fine but when i try to set the value to the column it wont change.

here is the code:

private void fixAlldates(DataGridView dataGridView2) 
    { 
        string aux1 = ""; 

        for (int x = 0; x < dataGridView2.Rows.Count; x++) 
        { 

            if (dataGridView2.Rows[x].Cells[4].Value.ToString() != null)
            { 
                aux1 = dataGridView2.Rows[x].Cells[4].Value.ToString();
                dataGridView2.Rows[x].Cells[4].Value = fixDate(aux1); 
            } 

            if (dataGridView2.Rows[x].Cells[5].Value.ToString() != null)
            { 
                dataGridView2.Rows[x].Cells[5].Value = fixDate(dataGridView2.Rows[x].Cells[5].Value.ToString()); 
            } 


            dataGridView2.Refresh(); 
              MessageBox.Show(fixDate(aux1); ----> shows result like i want ex: 2017-02-02 
            MessageBox.Show(dataGridView2.Rows[x].Cells[4].Value.ToString()); ----> shows 2070202 
        }       
    } 


    private string fixDate(string p) 
    { 

        if (p == null) return "No especificado"; 
            String fecha = "" + p.Substring(0, 4) + "-" + p.Substring(4, 2) + "-" + p.Substring(6, 2) + ""; 
            return fecha; 


    } 

sorry for my bad english , im a little bit rusty

Edit:

I fill the data with bindingSource.

   private void LlenarProductos(string rut)
    {

        this.rut = rut;
        POLbindingSource1.DataSource = null;
        dataGridView2.DataSource = null;
        DataClasses1DataContext dc = new DataClasses1DataContext();
        dc.CommandTimeout = 0;
        System.Data.Linq.Table<ASE_PRODUCTOASEGURADO> producto = dc.GetTable<ASE_PRODUCTOASEGURADO>();
        var todoprod = from p in producto
                       where p.RUT == int.Parse(rut)
                       select new
                       {
                           p.POLIZA,
                           p.SOCIO,
                           p.SUCURSAL,
                           p.COD_PROPUESTA,
                           p.FECHA_ALTA_COTI,
                           p.FECHA_ALTA_VCTO,
                           p.NOMBRE_PRODUCTO
                       };

        POLbindingSource1.DataSource = todoprod; // binding source 
        dataGridView2.DataSource = POLbindingSource1; // filll
        this.dataGridView2.Columns["POLIZA"].HeaderText = "Poliza";
        this.dataGridView2.Columns["Socio"].HeaderText = "Socio";
        this.dataGridView2.Columns["Sucursal"].HeaderText = "Sucursal";
        this.dataGridView2.Columns["COD_PROPUESTA"].HeaderText = "Propuesta";
        this.dataGridView2.Columns["FECHA_ALTA_COTI"].HeaderText = "Fecha Cotizacion";
        this.dataGridView2.Columns["FECHA_ALTA_VCTO"].HeaderText = "Fecha Vencimiento";
        this.dataGridView2.Columns["NOMBRE_PRODUCTO"].HeaderText = "Producto";
        //  fixAlldates(dataGridView2); 
    }
Artiom
  • 7,694
  • 3
  • 38
  • 45
Joaquin B.
  • 13
  • 1
  • 5
  • do you get an error? or have you traced it using breakpoint? – Masoud Andalibi Feb 02 '17 at 13:47
  • the code is executed normally? also can you show us how the datagridview is filled with data? – jambonick Feb 02 '17 at 13:47
  • 2
    It would be better to fix the *data* so that the fields contain actual DateTime objects instead of strings like `20170202`. All you'd need then would be to specify the correct format string for the grid column. How do you load the data? How do you bind it to the grid? – Panagiotis Kanavos Feb 02 '17 at 13:48
  • @Artiom that's what caused the problem in the first place. Instead of loading dates, the data contained a formatted string. *Don't* format the data before population. Let the format string do its job – Panagiotis Kanavos Feb 02 '17 at 13:53
  • @PanagiotisKanavos you are telling me that i can format the column to datetime? will it work if the comes from db without datetime format? – Joaquin B. Feb 02 '17 at 14:40
  • No, I'm saying that if the data that comes from the database is a DateTime or a number, you can use the `DataGridViewColumn.DefaultCellStyle` to apply styling, including setting the `Format` property to a format string, eg `Columns["FECHA_ALTA_COTI"].DefaultCellStyle.Format="YYYY-MM-DD"`. You won't need to convert anything – Panagiotis Kanavos Feb 02 '17 at 14:50
  • @PanagiotisKanavos It did not work, it think because it comes a a string from the db not as a datetime. – Joaquin B. Feb 02 '17 at 15:03

1 Answers1

0

From msdn https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.value(v=vs.110).aspx.

The Value property is the actual data object contained by the cell.

So basically the line MessageBox.Show(dataGridView2.Rows[x].Cells[4].Value.ToString()); is getting the value of the underlying data, whereas MessageBox.Show(fixDate(aux1); is actually formatting the date as you require.

You're overlooking the fact that although you're seeing the data in the grid in a specific way, you're not actually changing the data itself.

UPDATE

To actually edit the data in a cell see here

Community
  • 1
  • 1
Barry O'Kane
  • 1,189
  • 7
  • 12