DataTable诡异的事情

 今天在工作中遇到一个诡异的问题,代码如下:

private DataTable CovertDataType(DataTable orgDataTable, string colNames, string convertCol, YstPack.Common.ColumnType convertDataType)
        {
            DataTable dt = new DataTable();
            //克隆表结构
            dt = orgDataTable.Clone();
            foreach (DataColumn column in dt.Columns)
            {
                if (column.ColumnName == convertCol)
                {
                    //修改列类型
                    column.DataType = typeof(Guid);
                }
            }

            string[] colArry = colNames.Split(',');

            foreach (DataRow dr in orgDataTable.Rows)
            {
                DataRow newdr = dt.NewRow();
                foreach (string str in colArry)
                {
                    if (str == convertCol)//转换列数据类型
                    {
                        switch (convertDataType)
                        {
                            case YstPack.Common.ColumnType.唯一标识:
                                newdr[str] = new Guid(dr[str].ToString());
                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {

                        newdr[str] = dr[str];//不需要转化,直接赋值,但是这里就抛出异常!
                    }
                }
                dt.Rows.Add(newdr);
            }
            return dt;
        }

 

 
运行 foreach (string str in colArry)循环第一次能成功,但是第二次就报列“X”不属于表异常 !不知道这是不是一个bug!
后面把代码改成如下又正确了:
  

private DataTable CovertDataType(DataTable orgDataTable, string colNames, string convertCol, YstPack.Common.ColumnType convertDataType)
        {
            DataTable dt = new DataTable();
            try
            {
                //克隆表结构
                dt = orgDataTable.Clone();
                foreach (DataColumn column in dt.Columns)
                {
                    if (column.ColumnName == convertCol)
                    {
                        //修改列类型
                        column.DataType = typeof(Guid);
                    }
                }

                string[] colArry = colNames.Split(',');

                foreach (DataRow dr in orgDataTable.Rows)
                {
                    DataRow newdr = dt.NewRow();
                    for (int i = 0; i < colArry.Length; i++)
                    {

                        if (colArry[i].ToString() == convertCol)//转换列数据类型
                        {
                            newdr[i] = new Guid(dr[i].ToString());
                        }
                        else
                        {
                            newdr[i] = dr[i];
                        }

                    }
                    
                    dt.Rows.Add(newdr);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return dt;
        }


 

posted @ 2010-04-08 21:01  feiyang  阅读(456)  评论(6编辑  收藏  举报