I'm struggling find a specific answer to this question, therefore asking it myself...
I have a DataGridView with columns which has the following formatting applied to it:
DGV.Columns(3).DefaultCellStyle.Format = "C2"
On the form there is a text box in which the user enters a number and that value is then entered in one of the cells in DGV:
For Each dr As DataGridViewRow In DGV.Rows
dr.Cells("ColumnHeader").Value = TextBox.Text
Next
Before the above code is executed, this event occurs on the TextBox to format its value:
Private Sub TextBox_Leave(sender As Object, e As EventArgs) Handles TextBox.Leave
TextBox.Text = FormatCurrency(TextBox.Text)
End Sub
The text within the TextBox is displaying correctly as a currency, but when the code to put that into a cell in the DGV executes it fails saying the value is in incorrect format.
Is DGV.Columns(3).DefaultCellStyle.Format = "C2" different format to FormatCurrency(TextBox.Text)?
That is all wrong. "C2" is a numeric format string, so it will only work on numbers. A
Stringcontaining digit characters is not a number and aStringcontaining currency text definitely isn't. You need to get theStringfrom theTextBox, concert that to a number (probablyDecimalfor currency values) and then load that number into the grid. The grid converts that number, along with all the data, into text to display and will use your format string to do so:You would, presumably, already have validated the user input by this stage, so there's no possibility of
CDecthrowing an exception.If the intent is to display the data formatted as currency in both the grid and the
TextBoxthen you should get rid of yourLeaveevent handler and handle theValidatingandValidatedevents instead. The first will validate the input to ensure that it is numeric and the second will do the formatting:That code will allow the user to enter the currency symbol or not, but it will ensure that the format is currency with two decimal places once it loses focus. You would then need to allow for the formatting when copying to the grid: