C# 获取数据表列数据类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9660981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a DataTable Columns DataType
提问by Jeremy Thompson
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):
我期待以下行的结果包含有关 DataColumns 类型 (bool) 的信息:
?dt.Columns[0].GetType()
回答by Jeremy Thompson
What you want to use is this property:
您要使用的是此属性:
dt.Columns[0].DataType
The DataTypeproperty will set to one of the following:
该DataType属性将设置为以下之一:
Boolean
Byte
Char
DateTime
Decimal
Double
Int16
Int32
Int64
SByte
Single
String
TimeSpan
UInt16
UInt32
UInt64
回答by Natarajan Sambantham
dt.Columns[0].DataType.Name.ToString()
回答by Arpit Trivedi
You can get column type of DataTable with DataTypeattribute of datatable column like below:
您可以使用数据表列的DataType属性获取数据表的列类型,如下所示:
var type = dt.Columns[0].DataType
var type = dt.Columns[0].DataType
dt : DataTable object.
dt :数据表对象。
0 : DataTable column index.
0:DataTable 列索引。
Hope It Helps
希望能帮助到你
Ty :)
泰:)
回答by VDWWD
You could always use typeofin the if statement. It is better than working with string values like the answer of Natarajan.
您始终可以typeof在 if 语句中使用。它比使用像 Natarajan 的答案这样的字符串值更好。
if (dt.Columns[0].DataType == typeof(DateTime))
{
}
回答by Vinod Parmar
if (dr[dc.ColumnName].GetType().ToString() == "System.DateTime")
if (dr[dc.ColumnName].GetType().ToString() == "System.DateTime")

