הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Tuesday, February 28, 2012 5:02 PM
I have a DataGridView that is showing some weird numerical data. For example, my under lying datatable of the datagridview has 4.69, but the datagridview shows 4.6899999999999999995. What is weird is it doesn't do it for every cell in the column. For example, my underlying datatable has 2.66 and the datagridview cell shows 2.66, which is correct.
I should note that the underlying datatable column that contains the numbers is of floating point data type which comes from the database column and the datagridview column is TextBox. Here is what the values look like in the database, underlying datatable, and datagridview.
Why is this happening?
Thanks in advance!
Ryan
All replies (5)
Tuesday, February 28, 2012 5:16 PM ✅Answered
Welcome to floating point numbers. :-) Probably it does not have exactly 4.69 because it's of type Double. Not every number that can be displayed with a finite number of decimal places can be stored with limitted storage space. If you don't limit output to 2 decimal places, you get the result you see. If another display shows only two decimal places, it has already cut the output.
If you want exact decimal places also in the database, change the type to an appropriate format, like decimal in SQL server. Also update the type in the datatable.
Armin
Tuesday, February 28, 2012 6:46 PM ✅Answered
Go to DGV Column properties -> DefaultCellStyle -> Format -> Numeric -> Set 2 decimal places.
Hope that helps!
Amit Govil | Email
"Weeks of coding can save you hours of planning"
Tuesday, February 28, 2012 7:24 PM ✅Answered
Use this code:
Me.dataGridView1.Columns("YourColumnName").DefaultCellStyle.Format = "n2"
Mitja
Wednesday, May 30, 2012 7:02 PM ✅Answered
In order to fix this issue, I had to cast the value from float to varchar(10) back to float. This works great!
Ryan
Monday, March 5, 2012 3:59 PM
Hi Ryan,
Nice to see you.
How about the community members' suggestions?
How about your issue?
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.