Click here to Skip to main content
15,922,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Trying to assign a string "$57,834" to a DataGridViewTextBoxColumn "Amt":

For Each dgr As DataGridViewRow In dgvExpSubject.Rows
    dgr.Cells("Amt").Value = Format(amt, AG.MASK_DOLLARS)

Gives this error:

    DataGridView Exception: $57,834 is not a valid value for Int32

The Amt column type seems to be Int32. How can I assign string values to this DataGridViewTextBoxColumn?

What I have tried:

Tried stepping through which shows the ValueType as Int32.
Graeme_Grant 27-Nov-23 16:36pm    
You are missing code from your question. Click on the green Improve question link and post you column definitions.

You have defined that columns an an Int32 type, so you can only assign an integer value to it.
Share this answer
mmaucher 27-Nov-23 15:36pm    
The ColumnType is DataGridViewTextBoxColumn. Where is it picking up Int32?
Richard MacCutchan 27-Nov-23 15:58pm    
No idea. Check your code again.
The Format function you used in your code in VB is generally used for formatting strings, not for converting data types. you should convert the formatted string to the appropriate data type before assigning it to the cell. Assuming 'Amt' is expected to be a numeric value, you can use 'Decimal.TryParse' to convert the formatted string to a Decimal -
For Each dgr As DataGridViewRow In dgvExpSubject.Rows
    Dim formattedAmt As String = Format(amt, AG.MASK_DOLLARS)
    Dim parsedAmt As Decimal

    If Decimal.TryParse(formattedAmt, parsedAmt) Then
        dgr.Cells("Amt").Value = parsedAmt
        'Handle the case where your conversion fails, perhaps show an error message etc...
    End If
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900