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
相关产品推荐
相关产品推荐

