DataTable 更改在有数据列的类型方法

 /// <summary>
        /// 修改数据表DataTable某一列的类型和记录值(正确步骤:1.克隆表结构,2.修改列类型,3.修改记录值,4.返回希望的结果)
        /// </summary>
        /// <param name="argDataTable">数据表DataTable</param>
        /// <returns>数据表DataTable</returns>  

 private DataTable UpdateDataTable(DataTable argDataTable)
        {
            DataTable dtResult = new DataTable();
            //克隆表结构
            dtResult = argDataTable.Clone();
            foreach (DataColumn col in dtResult.Columns)
            {
                if (col.ColumnName == "RQ")
                {
                    //修改列类型
                    col.DataType = typeof(String);
                }
            }
            foreach (DataRow row in argDataTable.Rows)
            {
                DataRow rowNew = dtResult.NewRow();
                rowNew["DTBM"] = row["DTBM"];
                //修改记录值
                rowNew["RQ"] = Convert.ToDateTime(row["RQ"]).ToString("yyyy-MM-dd").ToString();
                rowNew["DWBM"] = row["DWBM"];
                rowNew["DWMC"] = row["DWMC"];
                rowNew["YYID"] = row["YYID"];
                rowNew["YYMC"] = row["YYMC"];
                rowNew["YXCL"] = row["YXCL"];
                dtResult.Rows.Add(rowNew);
            }
            return dtResult;
        }

posted @ 2010-04-14 11:25  mysun  阅读(25661)  评论(0编辑  收藏  举报