如何将DataGridView列数据类型从字符串转换为十进制? [英] How to convert DataGridView Column data type from string to decimal?

查看:179
本文介绍了如何将DataGridView列数据类型从字符串转换为十进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望找到一种简单的方法将datagridview中的整个列从字符串数据类型转换为小数。像这样简单吗?



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

推荐答案

没有转换类型这样的东西。您只能转换数据,某种类型的实例。您可以使用 System.Decimal.Parse 方法之一:

http://msdn.microsoft.com/en-us/library/system.decimal.parse%28v=vs.110% 29.aspx [ ^ ]。



您可以设置属性 ValueType ,但它只能用于过滤或排序相对于其单元格内容的列:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.valuetype%28v=vs .110%29.aspx [ ^ ]。



原则嗯,您也可以使自己的自定义列类型与其他列类型相同: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn%28v=vs.110%29.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]。



此列类型的功能取决于您的需求,但有一点是肯定的:它的 ValueType property应该返回 typeof(System.Decimal),它应该返回或接受此类型的值以及相应的属性 Value 专用的 DataGridViewCell 类型,你必须创建它,方法与这些相同:

HTTP://毫秒dn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell%28v=vs.110%29.aspx [ ^ ]。



-SA
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


尝试在sql中编写转换它。很容易
try to write convert it in sql .it will be easy


这篇关于如何将DataGridView列数据类型从字符串转换为十进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆