c# - How to convert data type items?
问题描述
I have a data table which has 12 items, I need to change the data type of 1st and 10th column
DataTable dtExcel = null;
dtExcel = GetExcelContainerFor<TBL_TICKETING_ALTERNATE_CREDIT_CARD_LIST>().ImportFromExcel(excelInput.DataVaultFileName, excelInput.IsHeaderRowExists);
so dtExcel is the data table which has the 12 items
I am looping through each data row and trying to convert it to Int32
dtExcel.Select().ToList().ForEach(dataRow =>
{
dataRow[0] = (Convert.ToInt32(dataRow[0]));
dataRow[9] = (Convert.ToInt32(dataRow[9]));
});
but as soon as the loops ends , the dtExcel data table has it's original data types.
I tried below coding as well but it didn't work as well(since it threw error as connot convert the datatype)
DataTable newTable = dtExcel.Clone();
for (int i = 0; i < dtExcel.Rows.Count; i++)
{
DataRow drNew = newTable.NewRow();
newTable.Columns[0].DataType = typeof(Int32);
newTable.Columns[9].DataType = typeof(Int32);
drNew.ItemArray = dtExcel.Rows[i].ItemArray;
newTable.Rows.Add(drNew);
}
How can I solve this issue? Do I need to create a new datable and loop through each items and then convert each and add?- if yes- can somebody share an example of how to achieve it?
解决方案
推荐阅读
- java - 不匹配 LIst<> 的类型及其元素 - Java
- reactjs - 使用状态添加项目 onClick 反应不起作用?
- python - 根据支出移动平均分配客户
- tensorflow - 相同的代码,相同的库,但为什么我在新笔记本电脑上的训练速度比旧笔记本电脑慢
- pandas - Redshift COPY 命令:频谱扫描错误 - 同一列的不同镶木地板类型
- go-gorm - 有没有办法结合范围和 OR 条件
- r - 在R中水平合并数据帧
- google-apps-script - 通过应用程序脚本从同一电子表格中的另一张工作表中获取值
- sql-server - 如何使用 substring 和 charindex 从第一列解析值并创建新列
- python - Pyppeteer: {'waitUntil': 'networkidle0'} 不等到页面加载完毕