0

I have a databound datagridview. When clicked, a second databound datagridview loads AND the numbers within the columns of this second datagridview are mulptiplied by 60. The problem is that when I click on the first datagridview a second, third etc time, the numbers are multiplied by 60 every time. I don't want this to happen. I tried this:

private Boolean _alreadyBeenMultiplied;

public Boolean AlreadyBeenMultiplied
{
     get { return _alreadyBeenMultiplied; }
     set
     {
          _alreadyBeenMultiplied = false;
     }

}

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

  if(_alreadyBeenMultiplied == false)
  {
      if (dgv.Columns[e.ColumnIndex].Name == "Hours")
      {
          if (dgv.Rows[e.RowIndex].Cells["Hours"].Value.ToString() != "0")
          {
             e.Value = Double.Parse(e.Value.ToString()) * 60;
             e.Value = (double.Parse(e.Value.ToString()));
              _alreadyBeenMultiplied = true;
          }
      }
  }

}

What happens is that when I click the first dgv the second time, the numbers all get divided by 60, returning to their original values. Also, the other columns involved in this code (I only showed the "Hours" column here for simplicity) are not effected at all, not even on initial click. If anyone can help, I would really appreciate it. I'm almost positive it's a logic problem.

Tatiana Laurent
  • 281
  • 1
  • 4
  • 13
  • Why are you setting e.Value to the same value twice for the same event? Also, your hrsToMins variable assignment statement should throw an exception if the value in your table isn't a number. – Rob G Dec 11 '13 at 17:38
  • Also is this a custom control inheriting datagridview? – Rob G Dec 11 '13 at 17:41
  • 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 ToString method – Tatiana Laurent Dec 11 '13 at 17:48
  • The datagridview is added to a custom user control – Tatiana Laurent Dec 11 '13 at 17:50
  • Is there anywhere you can post the rest of your code so we could try it out? – Rob G Dec 11 '13 at 17:53

0 Answers0