Avatar of Jimbo99999
Flag 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?

.NET Programming

Avatar of undefined
Last Comment

8/22/2022 - Mon
David Johnson, CD

is the source a string or a decimal?
Scott McDaniel (EE MVE )

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).

Thanks for your responses.  Please don't mark as neglected as I was off for the holiday.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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.