Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm hoping to find a simple way to convert an entire column in my datagridview from a string data type to a decimal. Something simple like this maybe?

DataGridView1.Columns(4).ValueType = Decimal
Posted
Updated 27-Feb-17 23:45pm

There is no such thing as "convert type". You can "convert" only data, an instance of some type. You can use one of the System.Decimal.Parse methods:
http://msdn.microsoft.com/en-us/library/system.decimal.parse%28v=vs.110%29.aspx[^].

You can set the property ValueType, but it can only by used for filtering or sorting the columns with respect to the contents of their cells:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.valuetype%28v=vs.110%29.aspx[^].

In principle, you can also make your own custom column type the same was other column types are made: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn%28v=vs.110%29.aspx[^].

The functionality of this column type would depend on what you want, but one thing is certain: its ValueType property should return typeof(System.Decimal), and it should return or accept values of this type with the corresponding property Value of the specialized DataGridViewCell type, which you also have to create, the same way as these:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v4
try to write convert it in sql .it will be easy
 
Share this answer
 
Comments
CHill60 28-Feb-17 8:25am    
Couple of issues with your solution:
1. The question is not tagged "Sql" so your suggestion may be completely off-base - we simply don't know how the DataGridView is being populated.
2. It might well be easy to provide this conversion but we have no idea what expertise the OP has, so provide examples rather than bald statements like this.
3. Better advice to the OP would have been to question how the column was a string in the first place, it should be set up as the correct type.
4. The question is over 3 years old and your solution has added no value to the thread

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