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.

Recommended Answers

All 4 Replies

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).

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.

Nothing to help me ???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.