[英]Adding a column of type enum to datatable column
Given: 鉴于:
namespace VolumeProfileStateEntity
{
public enum VolumeProfileState
{
AboveHigh,
AboveValue,
AtOrNearValue,
BelowValue,
BelowLow
}
}
I want to add a column
of type VolumeProfileState
: 我想添加一个类型为
VolumeProfileState
的column
:
DataTable dt1 = new DataTable("State");
Adding columns
to the DataTable
is fine until I try to add a column of type VolumeProfileState
: 在尝试添加
VolumeProfileState
类型的列之前,可以将columns
添加到DataTable
中很好:
// Add columns to the DataTable.
var dc1 = dt1.Columns.Add("TimeFrame",
System.Type.GetType("System.String"));
dc1.ReadOnly = true;
dt1.Columns.Add("Hi",
System.Type.GetType("System.Double"));
dt1.Columns.Add("Low",
System.Type.GetType("System.Double"));
dt1.Columns.Add("Middle",
System.Type.GetType("System.Double"));
dt1.Columns.Add("State",
System.Type.GetType("VolumeProfileStateEntity.VolumeProfileState"));
Exception thrown: 'System.ArgumentNullException' in System.Data.dll
I know I can make the column of type 我知道我可以将列类型
System.Type.GetType("System.Int32"));
but then I have problems when entering data because Int32
is not IConvertible
to enum
types AFAIK. 但是然后输入数据时出现问题,因为
Int32
不能IConvertible
为enum
类型AFAIK。
Am I doing something wrong, or is it only possible to add column types of System.X
and not enums
? 我是在做错什么,还是只能添加
System.X
列类型而不是enums
?
you should use a database column as int, then cast the value to an enum when retrieving it. 您应该将数据库列用作int,然后在检索时将值强制转换为枚举。 You could also do the column as a string then cast to an enum when retrieving it.
您也可以将列作为字符串进行处理,然后在检索时将其强制转换为枚举。 but int storage is smaller.
但是int存储空间较小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.