C# 修改DataTable列 类型 并从新赋值

   DataTable dt = ds.Tables[0];

            DataTable dtResult = new DataTable();
            //克隆表结构
            dtResult = dt.Clone();
            foreach (DataColumn col in dtResult.Columns)
            {
                if (col.ColumnName == "isCreditAll")
                {
                    //修改列类型
                    col.DataType = typeof(string);
                }
            }
            foreach (DataRow row in dt.Rows)
            {
                DataRow newDtRow = dtResult.NewRow();
                foreach (DataColumn column in dt.Columns)
                {
                    if (column.ColumnName == "isCreditAll")
                    {
                        newDtRow[column.ColumnName] = Convert.ToString(row[column.ColumnName]);
                    }
                    else
                    {
                        newDtRow[column.ColumnName] = row[column.ColumnName];
                    }
                }
                dtResult.Rows.Add(newDtRow);
            }

            for (int i = 0; i < dtResult.Rows.Count; i++)
            {
                if (dtResult.Rows[i]["isCreditAll"].ToString() == "false")
                {
                    dtResult.Rows[i]["isCreditAll"] = "1";
                }
                else
                {
                    dtResult.Rows[i]["isCreditAll"] = "0";
                }


            }

 

posted @ 2017-08-31 14:11  乐途  阅读(4082)  评论(0编辑  收藏  举报