You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DataGridView单元格格式化丢失小数精度,如何实现Excel式编辑还原?

DataGridView单元格编辑时还原原始精度值的问题

我使用二维Double数组填充DataGridView,将单元格默认格式设置为N1后,原始值1393.832900298831会显示为1,393.8,但网格中存储的实际值也变成了1393.8,丢失了原始的精度数据。

我需要实现类似Excel的单元格显示/编辑逻辑:用户进入单元格编辑模式时,当前显示的1393.8能还原为原始的1393.832900298831。

CellFormatting事件无法满足需求,因为我需要在值修改后仍保留格式化的显示效果。

我尝试通过CellBeginEdit事件将单元格值重置为数组中的原始值,但没有任何效果:

' 该数组在其他位置初始化/填充
Protected rInputValues(,) as Double

Private Sub DgvInput_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DgvInput.CellBeginEdit

    ' 无任何效果,也不报错
    DgvInput.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = rInputValues(e.RowIndex, e.ColumnIndex)

End Sub

JIMI提供的解决方案是使用EditingControlShowing事件,能够完美实现需求:

Private Sub DgvInput_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DgvInput.EditingControlShowing

    If (DgvInput.CurrentCell.Value Is Nothing) Then
        Exit Sub
    End If
    e.Control.Text = DgvInput.CurrentCell.Value.ToString()
End Sub

内容的提问来源于stack exchange,提问作者20 Ton Squirrel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 06:05:20