VB.net DataGridView 更改 Cell.FormattedValue

标签 vb.net datagridview

如何更改用户双击单元格后出现在 EditMode 中的字符串/值 (cell.FormattedValue)?

即值为“5”,格式化值为“5 pcs”,这会导致验证错误。所以我不想将显示给用户的字符串从“5 个”替换回“5”。 然后它应该像用户 simpy 开始写“5”并按 Enter 时一样工作。

这是不可能的,FormattedValue 是只读的:

Me.dgwNest1.Rows(e.RowIndex).Cells(e.ColumnIndex).FormattedValue = 5

Cell.Value 不可用,因为它只会更改已编辑字符串的值部分。

问候,

伦敦银行同业拆借利率

最佳答案

只需调用引发 DataGridView.CellFormatting 事件的 FormattedValueType 属性,然后在事件代码中更改单元格样式。

FormattedValue 只是 Value 属性的格式化表示。 FormattedValue 表示由 FormattedValueType 属性指定。

如果您更改 FormattedValueType 属性,它将更改它呈现给用户的方式而不更改值。 =)

MSDN 中的这些注释向您展示了如何编辑 FormattedValue:

The Value property is the actual data object contained by the cell, whereas the FormattedValue is the formatted representation of this object. The ValueType and FormattedValueType properties correspond to the data types of these values, respectively.

Getting the value of this property calls the GetFormattedValue method to convert the cell value into an equivalent display value of the type indicated by the FormattedValueType property. This raises the DataGridView.CellFormatting event, which you can handle to customize the value conversion.

请参阅 MSDN 上有关 DataGridViewCell.FormattedValue 属性的注释 here.

当引发 CellFormatting 事件时,您可以执行如下操作:

Private Sub dgvMyData_CellFormatting(sender As System.Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvMyData.CellFormatting
    Dim MyStyle As New System.Windows.Forms.DataGridViewCellStyle

    MyStyle.Format = "00 ct"
    'or to remove the formatting:
    MyStyle.Format = "00"

    e.CellStyle = MyStyle
End Sub

关于VB.net DataGridView 更改 Cell.FormattedValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26034562/

相关文章:

c# - DataGridView 不显示我的列表

vb.net - 如何在 vb .net 中获取任何已连接打印机的型号

mysql - 在 VB.NET 中为 MySql 和 MSSql 编程

.net - 如何安全地将文件保存到磁盘而没有被 Windows 拒绝的风险?

c# - 如何使用 c# winform 从 datagridview 中编辑的单元格获取数据?

mysql - 在 vb.net 中将多个表中的列获取到 1 个 datagridview 中

c# - 将 DataGridView 绑定(bind)到数据库中的表

vb.net - 如何在使用 VB.NET 在 DataGridView 中编辑单元格时获取当前列索引

vb.net - CStr() Str() .ToString()

vb.net - 与 VB.NET 相比,为什么缺乏对 C# 的 IntelliSense 支持?