如何使用VB.Net防止DataGridView中的舍入数字 [英] How to Prevent rounding numbers in DataGridView using VB.Net
问题描述
我在windows窗体vb.net应用程序中有一个datagridview。我的目标是让任何数字超过0.999999999999 12位有效数字舍入到0.99999999999 11位有效数字。我想阻止vb.net将这些值舍入为1.这就是我实现这个的方法,但我无法弄清楚要使用哪个DataGridView事件处理程序。
I have a datagridview in a windows forms vb.net application. My goal is to have any number over 0.999999999999 12 significant digits to be rounded down to 0.99999999999 11 significant digits. I want to prevent vb.net from rounding these values up to 1. This is how I'm implementing this but I can't figure out which DataGridView Event handler to use.
If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value <= 0.999999999999 Then
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0.99999999999
End If
推荐答案
您可以为DataGridView.CellFormatting事件添加处理程序;这样你就可以自定义格式化的值(DataGridView将为特定单元格显示数据的方式)。
它可能会给出类似的结果:
You could add an handler to your DataGridView.CellFormatting event; this way you can customize the formatted value (the way the DataGridView will present the data for a specific cell).
It may give something like:
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If (e.ColumnIndex = 4) Then '' Here I assume that your column is the fifth
If (e.Value Is Not Nothing) Then
Dim stringValue As String = e.Value.ToString("0.00000000000")
e.Value = stringValue
e.FormattingApplied = True
End If
End If
End Sub
希望这会有所帮助。我为可以用VB做的任何错误事先道歉,因为我大约十年前放弃了这种语言。
Hope this helps. I apologize in advance for any mistake I could have done with VB, as I gave up this language about ten years ago.
这篇关于如何使用VB.Net防止DataGridView中的舍入数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!