C# 根据实体将DataTable拆分成表头表体


             //数据源
             DataTable dt ;
             //拆分后的表头
             DataTable DtHead ;
             //拆分后的表体
            DataTable DtBody ;

 public void SplitDataTable(DataTable dt, out DataTable DtHead, out DataTable DtBody)
        {
            //拆分后的表头
            DtHead = new DataTable();
            foreach (PropertyInfo item in new Kj_Order_Raw_Head().GetType().GetProperties())
            {
                DtHead.Columns.Add(item.Name, item.PropertyType);
            }
            //拆分后的表体
            DtBody = new DataTable();
            foreach (PropertyInfo item in new Kj_Order_Raw_List().GetType().GetProperties())
            {
                DtBody.Columns.Add(item.Name, item.PropertyType);
            }
            //赋值
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow NewHeadRow = DtHead.NewRow();
                DataRow NewBodyRow = DtBody.NewRow();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    string ColumnName = dt.Columns[j].ColumnName;
                    if (DtHead.Columns.Contains(ColumnName))
                    {
                        NewHeadRow[ColumnName] = dt.Rows[i][ColumnName];
                    }
                    if (DtBody.Columns.Contains(ColumnName))
                    {
                        NewBodyRow[ColumnName] = dt.Rows[i][ColumnName];
                    }
                }
                DtHead.Rows.Add(NewHeadRow);
                DtBody.Rows.Add(NewBodyRow);
            }
        }

 

     

 


 

posted @ 2019-08-22 11:07  Vaynedy  阅读(793)  评论(0编辑  收藏  举报