I can not assign a double value to a datagrid, it takes it as int

1

public static int contador_fila = 0;

private void btn_Colocar_Click(object sender, EventArgs e)
{
    if (Utilidades.ValidarFormulario(this,errorProvider1) == false)
    {
       bool existe = false;
       int num_fila =0;

      if (contador_fila == 0)
      {
          dataGridView1.Rows.Add(txt_CodigoProducto.Text, txt_Descripcion.Text, txt_Precio.Text, txt_Cantidad.Text);

          double importe = Convert.ToDouble(dataGridView1.Rows[contador_fila].Cells[2].Value) * Convert.ToDouble(dataGridView1.Rows[contador_fila].Cells[3].Value);

          dataGridView1.Rows[contador_fila].Cells[4].Value = importe;

          contador_fila++;

      }
      else
      {
          foreach (DataGridViewRow Fila  in dataGridView1.Rows)
          {
              if (Fila.Cells[0].Value.ToString() ==txt_CodigoProducto.Text)
              {
                  existe = true;
                  num_fila = Fila.Index;
              }
          }

          if (existe == true)
          {   // le cambie el txt de cantidad a precio
              dataGridView1.Rows[num_fila].Cells[3].Value = (Convert.ToDouble(txt_Cantidad.Text) + Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[3].Value)).ToString();

              double importe = Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[2].Value) * Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[3].Value);

              dataGridView1.Rows[num_fila].Cells[4].Value = importe;
          }
          else
          {
              dataGridView1.Rows.Add(txt_CodigoProducto.Text, txt_Descripcion.Text, txt_Precio.Text, txt_Cantidad.Text);
              // movi la posicion de celda
             double importe = Convert.ToDouble(dataGridView1.Rows[contador_fila].Cells[2].Value) * Convert.ToDouble(dataGridView1.Rows[contador_fila].Cells[3].Value);

            dataGridView1.Rows[contador_fila].Cells[4].Value = importe;

            contador_fila++;
          }
      }

  }
}

When I assign it to 15.50, it takes it as 1550:

    
asked by Ed Brennenburg 19.03.2018 в 21:32
source

1 answer

1

You should work with strings, this will give you a format (called a mask), where you can access this article from Microsoft , to give a mask to the data type: double .

As for example:

public string ObtenerStringDeDouble(double valor)
{
    return value.ToString("0.00", CultureInfo.InvariantCulture); // Formato 0.00
}

Now if you want to perform an operation with doubles types I recommend storing the information in an object (such as a matrix).

Greetings, I hope I have helped you.

    
answered by 19.03.2018 / 21:52
source