/// <summary>
        /// 将IList集合转化为DataTable
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public DataTable ToTagInfoDataTable(IList<GkOptim_AddEquipment> list)
        {
            DataTable result = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    result.Columns.Add(pi.Name, pi.PropertyType);
                }
                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    result.LoadDataRow(array, true);
                }

                result.Columns.Add("RowNum", Type.GetType("System.Int32"));//向result里增加多一列   
                int RowsCount = result.Rows.Count;
                for (int j = 0; j < RowsCount; j++)//为该列增加相应的数值   
                {
                    IList<GkOptim_AddEquipment> equipmentlist = new GKOptimalizeBLL.AddEquipment().GetEquipmentListByUnitEquOrgID(result.Rows[j]["EquOrgID"].ToString(), int.Parse(result.Rows[j]["Unit"].ToString()));
                    result.Rows[j]["RowNum"] =  equipmentlist.Count;
                }
            }
            else
            {
                result.Columns.Add("Code");
                result.Columns.Add("EquOrgID");
                result.Columns.Add("EquName");
                result.Columns.Add("EquVarName");
                result.Columns.Add("EquTag");
                result.Columns.Add("Unit");
                result.Columns.Add("EquOrgName");
                result.Columns.Add("IsUse");
                result.Columns.Add("EquSchemeID");
                result.Columns.Add("ByOrder");
                result.Columns.Add("EquSchemeName");
                result.Columns.Add("EquShortTag");
                result.Columns.Add("RowNum");
            }
            return result;
        }

 

posted on 2013-05-18 22:50  莫等闲也  阅读(2751)  评论(0编辑  收藏  举报