C#中的转换资料类型问题 [英] Casting data type problem in C#

查看:72
本文介绍了C#中的转换资料类型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您从DataTable.Rows []读取数据并将其加载到控件中时,如果目标数据类型与Db数据类型匹配,则无需转换数据类型就可以做到. dt.Rows [0] [0] .Value返回对象数据类型,该类型为
最通用的数据类型.如果数据库列是十进制的,并且要加载到它的控件也是十进制,它将自动将对象数据类型转换为十进制,而不必使用Convert.ToDecimal或类似的东西.它适用于所有数据类型.
如果是,则可以在C#中执行此操作.

when you are reading data from a DataTable.Rows[], and loading it into a control, you can do it without converting the datatype if the target datatype matches the Db datatype. the dt.Rows[0][0].Value returns an object data type, which is
the most general datatype. if the database column is decimal and the control you are loading it to is also decimal, it will automatically cast the object datatype to a decimal without having to use Convert.ToDecimal or anyting like that. it applies to all datatypes.
It is possible in C# if yes then how to do this.

推荐答案

我认为这篇出色文章的第一个示例可以回答您的问题:
使用Windows Forms 2.0进行数据绑定:使用.NET编程智能客户端数据应用程序 [
I think one of the first examples of this excellent article answers your question:
Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET[^]

So yes it''s possible in c# - no explicit casting required.

Best regards
Espen Harlinn


VB.NET会自动进行强制转换. C#没有.这些控件将根据被告知的预期内容,使用反射来尝试确定数据类型是什么.您可以编写很多反射代码,但这几乎没有道理.您可以使用tryParse来确定某物是否是特定类型,如bool.TryParse和int.TryParse中所述,没有一个步骤返回类型化对象"方法,因为每个方法只有一个返回类型.
VB.NET does automatic casting. C# does not. Those controls would use reflection to try to work out what the data type is, based on having been told what to expect. You can write a lot of reflection code, but it rarely makes sense. You can find out if something is a specific type using TryParse as in bool.TryParse and int.TryParse, there''s no one step "return the typed object" method, as each method has only one return type.


这篇关于C#中的转换资料类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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