datagridView CellFormatting and cellParsing for formatted values and stored valu

I am using VS2005 datagridview. I am programming in C#. The datagridview is bound to a table in a dataset.

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.
[2132 byte] By [sarama] at [2007-11-20 10:40:14]
# 1 Re: datagridView CellFormatting and cellParsing for formatted values and stored valu
I found the solution to my problem. The cellFormatting event works fine. But I had to use the CellValueChanged event instead of CellParsing event.

Since the program thinks that the value that I entered is the Value, not the Formatted Value of the cell. I reset the Value of the cell in the CellValueChanged event.

/*Declared boolean so that CellValueChanged event does not run in loop because the values are reset in this event.
*/
private bool bCellValueChanged = false;

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)

{

if (dataGridView1.Columns[e.ColumnIndex].Name == "BHTemp" &&
bCellValueChanged == false)

{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == null)
{
bCellValueChanged = true;
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 0;
bCellValueChanged = false;
}
else
{
double f = double.Parse(dataGridView1.Rows[e.RowIndex].Cells [e.ColumnIndex].Value.ToString());
bCellValueChanged = true;
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Temperature_Parse_dgv(f);
// Temperature_Parse_dgv converts value from Celsius to Kelvin.
bCellValueChanged = false;
}
}

}
sarama at 2007-11-9 11:35:37 >