zoukankan      html  css  js  c++  java
  • 将Ilist集合转化为DataTable,并向DataTable中添加一列赋值

          /// <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;
            }
    
  • 相关阅读:
    java连接Ldap
    REGEXP_LIKE,REGEXP_INSTR,REGEXP_SUBSTR,REGEXP_REPLACE
    正则表达式学习笔记
    旋转的播放按钮
    折叠table中的tr
    css选择器.md
    清除浮动.md
    jquery-validate使用.md
    EL表达式.md
    C标签的使用.md
  • 原文地址:https://www.cnblogs.com/yisheng/p/3086309.html
Copyright © 2011-2022 走看看