datagridView CellFormatting and cellParsing for formatted values and stored valu
I have a column that has temperature units. The unit stored in the dataset table is in Kelvin, but users can choose their displayed units and the displayed values will change accordingly.
For example, when I choose displayed unit to be Celsius, in the datagridview cellFormatting event, I did the conversion so that 313K will be displayed as 40 (Celsius), 328K will be displayed as 55 (Celsius). In the datagridview cellParsing event, I did a backwards conversion to convert user input values in Celsius to Kelvin.
But the problem is, whenever I leave the cell, the input value is thought to be in Kelvin and another value in Celsius is displayed.
For example, I enter 50 in a cell (thinking it should be Celsius), I am expecting the displayed value should still be 50, but the stored value in the dataset table should be 323. But in this case, as soon as user leaves the cell, the displayed value changes to -223 (50 - 273 in Celsius) and the value in the datatable is 50 (Kelvin).
I don't know how to get around the problem so that the program will think that the value that I inputted in the cell is the displayed value, not the actual value. Am I missing another event? What did I do wrong?
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "BHTemp")
{
Temperature_Format_dgv(sender, e);
//this converts values from datatable to display in Celsius
e.FormattingApplied = true;
}
}
private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "BHTemp")
{
Temperature_Parse_dgv(sender, e);
//this converts values from Celsius to Kelvin
e.ParsingApplied = true;
}
}
Thanks for your help.

