Link to home
Start Free TrialLog in
Avatar of Jimbo99999
Jimbo99999Flag for United States of America

asked on

VB.Net - DataGrid Change ColumnType

Good Day Experts!

I gave it a valiant effort but I am really out of ideas.  I have a DataGrid with double data values in 5 columns as received from a DataTable.  I do not have the DataGrid column formatted as currency.

I need to be able to iterate through the DataGrid and based on a string value in the first column put a $ or £ in front of the double value.  Here is what I am trying:

Dim Amount As String = CStr(DataGridRow.Cells(4).Value)
DataGridRow.Cells(4).Value = "$" & Amount

Unfortunately I am getting an error --> Not a valid value for type decimal

It feels like the DataGrid is holding onto the type "assigned" from the DataTable but I am not sure.

Can you help with ideas?

Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

is the source a string or a decimal?
Assuming you're using a DataGridView:

If DataGridRow.Cells(4) is a Numeric value, then you cannot add the currency symbol. You can format it as Currency:

DataGrid.Columns(4).DefaultCellStyle.Format = "c"

It feels like the DataGrid is holding onto the type "assigned" from the DataTable but I am not sure.
AFAIK, you cannot change the Datatype of a Datagridview Column after it's created. You'd have to instead create a new column with your desired type, and fill it as needed. You can set the Column DataPropertyName to the column in your underlying dataset, and the grid should show the values in that column (assuming they're compliant with the column's datatype, of course).
Avatar of Jimbo99999


Thanks for your responses.  Please don't mark as neglected as I was off for the holiday.
Avatar of Jimbo99999
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I didn't want to delete the question since I found a solution on my own.  I wanted to share it for others to use that may be in the same situation.