Manipulating numeric data in a DataGridViewCell

203 views Asked by At

I have a datagridview that is populated with data. The data are numbers. I want to get the value of certain cells, multiply that value by 60 and print the new value into the cells but all I can seem to do is get a string value. This is what I have so far:

private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
   double hrsToMins = (double.Parse(dgv.Rows[e.RowIndex].Cells["Hours"].Value.ToString())*60);

   if (dgv.Columns[e.ColumnIndex].Name == "Hours")
   {
      if (dgv.Rows[e.RowIndex].Cells["Hours"].Value.ToString() != "0")
         hoursToMins = Convert.ToDouble(e.Value);
   }
}

This doesn't change anything. What am I doing wrong?

1

There are 1 answers

0
Tatiana Laurent On

This is what works:

e.Value = Double.Parse(e.Value.ToString()) * 60; 
// Converts the string to a double using the static Parse method 
// and multiplies it by 60.


e.Value = (double.Parse(e.Value.ToString()));
// Converts the double back to a string using the ToString method