954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Datagridview cell value not save exactly to sql table

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.

alinutzu
Newbie Poster
4 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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

Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

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

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

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.

alinutzu
Newbie Poster
4 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Nothing to help me ???

alinutzu
Newbie Poster
4 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: