1
我想创建一个DataTable,但我不知道在编译时列的类型。因此,我希望在运行时决定列类型。是否可以在运行时在DataTable中定义列类型?
我已经试过:
首先,我从Excel中获取一些数据:
Type tempCheck = (dynamic)(xlRange.Cells[1, currentColumnsCount] as Excel.Range).Value2.GetType();
然后我尝试使用类型来创建一个列。
myDataTable.Columns.Add(columnName, typeof(tempCheck.GetType());
我得到的错误:
The type or namespace name 'tempCheck' could not be found (are you missing a using directive or an assembly reference?)
仔细查看第一个代码示例,'tempCheck'已经是类型了,正确的答案就是'myDataTable.Columns.Add(columnName,tempCheck);' –
@ScottChamberlain oops,是的,你是对的。非常感谢。 – vcsjones