gpt4 book ai didi

VB.net DataGridView 更改 Cell.FormattedValue

转载 作者:行者123 更新时间:2023-12-02 04:52:56 25 4
gpt4 key购买 nike

如何更改用户双击单元格后出现在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com