Get a DataTable Columns DataType

六眼飞鱼酱① 提交于 2019-12-03 04:43:47

问题


DataTable dt = new DataTable();  
dt.Columns.Add(new DataColumn(gridColumn1, typeof(bool)));

I was expecting the result of the below line to include info about the DataColumns Type (bool):

?dt.Columns[0].GetType()

回答1:


What you want to use is this property:

dt.Columns[0].DataType

The DataType property will set to one of the following:

Boolean
Byte
Char
DateTime
Decimal
Double
Int16
Int32
Int64
SByte
Single
String
TimeSpan
UInt16
UInt32
UInt64

DataColumn.DataType Property MSDN Reference




回答2:


dt.Columns[0].DataType.Name.ToString()



回答3:


You could always use typeof in the if statement. It is better than working with string values like the answer of Natarajan.

if (dt.Columns[0].DataType == typeof(DateTime))
{
}



回答4:


You can get column type of DataTable with DataType attribute of datatable column like below:

var type = dt.Columns[0].DataType

dt : DataTable object.

0 : DataTable column index.

Hope It Helps

Ty :)



来源:https://stackoverflow.com/questions/9660981/get-a-datatable-columns-datatype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!