Hi.
I have an bounded datagridview with cell decimal different formating (N2 and N4).
If I enter a 4.5265 value to a cell with N2, I formated this to appear 4.53 ... and this is wonderfull. My problem is when I try to save data ... in my sql table not saved 4.53 value, saved 4.5265 ... and this is my big problem.

How this is possible ? What I wrong ? Please help me.

Just because you formatted the display value doesn't change the value. If you want it to save the rounded value you'll have to actually round the value.

You'll want to take a look at Math.Round()

Try to set a database field, use a decimal and set 2nd number to number of decimal places:
decimal(18,2) -> 2nd number is a number of numbers behind the decimal delimiter.

NOTE: anyway if you leave all the decimals in the database, this doesnt change anything. When you retreive data back out, you do the rouning, like Momerath showed (by using Math.Round method), or your solution (using N2 in the stirng).

Edited 5 Years Ago by Mitja Bonca: n/a

Just because you formatted the display value doesn't change the value. If you want it to save the rounded value you'll have to actually round the value.
You'll want to take a look at Math.Round()

Thanks for response but don't work.
This is my code :

private void dgRec_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow row = dgRec.Rows[e.RowIndex];
switch (e.ColumnIndex)
{
case 1:
e.FormattingApplied = true;
e.Value = vIdRec.ToString();
break;
case 2:
e.FormattingApplied = true;
e.Value = strGest;
break;
case 5:
try
{
e.Value = Math.Round(Convert.ToDecimal(e.Value), 2, MidpointRounding.AwayFromZero).ToString();
}
catch
{
e.Value = "0.00";
}
break;/*
}
}

In my sql table not save rounded value . Why ? This datagridview (dgRec) are bounded.

Edited 3 Years Ago by Nick Evan: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.