Modifying DataGridView VB.net Cell.FormattedValue Data
How do I change the string / value (cell.FormattedValue) that appears in EditMode after the user has double clicked on a cell?
those. value "5" generated value "5 pieces" and this causes a validation error. So I don't want to replace the string that appears to the user from "5 pieces" to "5". Then it should work the same as if the simpy user starts typing "5" and presses Enter.
This is not possible, FormattedValue is ReadOnly:
Me.dgwNest1.Rows(e.RowIndex).Cells(e.ColumnIndex).FormattedValue = 5
Cell.Value is not used as it only changes part of the value of the edited string.
Hello,
Libor
source to share
Just call the FormattedValueType property, which raises the DataGridView.CellFormatting event, and then changes the style of the cell in the event code.
FormattedValue is the only formatted representation of the Value property. The FormattedValue view is set by the FormattedValueType property.
If you change the FormattedValueType property, it changes how it is presented to the user without changing the value. =)
These notes from MSDN show you how to edit FormattedValue:
The Value property is the actual data object contained in the cell, whereas FormattedValue is the formatted representation of that object. The ValueType and FormattedValueType properties correspond to the data types of these values, respectively.
Retrieving the value of this property calls the GetFormattedValue method to convert the cell value to an equivalent display value of the type specified by the FormattedValueType property. This raises the DataGridView.CellFormatting Event, which you can handle to customize the value conversion.
See the notes on MSDN for the DataGridViewCell.FormattedValue property here.
When the CellFormatting event is raised, you can do something like this:
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
source to share