修改DataTable中的列的数据类型

修改DataTable中的列的数据类型


                            DataTable dt = AuctionOrders.PayCarPrice(uid, payStatus, pageIndex, pageSize);

                            if (dt.Rows.Count > 0)
                            {
                                //新表
                                DataTable newDt = new DataTable();
                                List listColums = new List();
                                //复制表结够
                                newDt = dt.Clone();

                                //新表中的列数据类型为Decmail的改为string
                                foreach (DataColumn col in newDt.Columns)
                                {
                                    listColums.Add(col.ColumnName);
                                    if (col.DataType.FullName == "System.Decimal")
                                    {
                                        col.DataType = Type.GetType("System.String");
                                    }
                                }

                                foreach (DataRow row in dt.Rows)
                                {
                                    DataRow newDtRow = newDt.NewRow();
                                    foreach(DataColumn column in dt.Columns)
                                    {
                                        if (column.DataType.FullName == "System.Decimal")
                                        {
                                            newDtRow[column.ColumnName] = (Convert.ToDecimal(row[column.ColumnName]) / 10000).ToString("c") + "万";
                                        }
                                        else
                                        {
                                            newDtRow[column.ColumnName] = row[column.ColumnName];
                                        }
                                    }
                                    newDt.Rows.Add(newDtRow);
                                }



                                var obj = new { state = true, message = newDt };
                                Response.Write(JsonConvert.SerializeObject(obj));
                            }
                            else
                            {
                                var obj = new { state = false, message = "没有查询到信息" };
                                Response.Write(JsonConvert.SerializeObject(obj));
                            }


你可能感兴趣的:(C#)