修改已有数据的DataTable的字段类型

时间:2020-12-02 12:21:35

//将int类型的字段改为string类型          

DataTable dtResult = new DataTable();
            dtResult = dt.Clone();
            foreach (DataColumn col in dtResult.Columns)
            {
               col.DataType = typeof(String);
            }
            for (int i = 0; i < dt.Rows.Count ; i++)
            {
                DataRow rowNew = dtResult.NewRow();
                for (int j = 0; j < dt.Columns.Count ; j++)
                {
                        if (Convert.ToInt32(dt.Rows[i][j]) == 0)
                        {
                            rowNew[j] = "不开考";
                        }
                        else if (Convert.ToInt32(dt.Rows[i][j]) == 1)
                        {
                            rowNew[j] = "开考";
                        }             
                }
                dtResult.Rows.Add(rowNew);
            }