• 多个不同的表合并到一个datatable中,repeater在绑定datatable


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    namespace LZserverList
    {
        public partial class LZList : System.Web.UI.Page
        {
            SqlConnection conn = new SqlConnection("server=.;database=ShifanRoadLZ0818;uid=sa;pwd=123456;");
            //处罚表查询
            string sqlcf = @"select  DJB_CF.CaseDistrict, DJB_CF.CaseYear,DJB_CF.CaseMonth, DJB_CF.CaseMoney,DJB_CF.TreeType  from DJB_CF";
            //赔补偿表查询
            string sqlpbc = @"select  DJB_PBC.CaseDistrict,DJB_PBC.CaseYear,DJB_PBC.CaseMonth, DJB_PBC.CaseMoney,DJB_PBC.TreeType  from DJB_PBC";
            //强制表查询
            string sqlqz = @"select  DJB_QZ.CaseDistrict,DJB_QZ.CaseYear,DJB_QZ.CaseMonth, DJB_QZ.CaseMoney,DJB_QZ.TreeType  from DJB_QZ";
            //许可表查询
            string sqlxk = @"select  DJB_XK.CaseDistrict as cd,DJB_XK.CaseYear as cy,DJB_XK.CaseMonth as cm, DJB_XK.CaseMoney as cmy,DJB_XK.XKType as ct  from DJB_XK";
         
            protected void Page_Load(object sender, EventArgs e)
            {
                Bind();
            }
            //绑定repeater
            public void Bind()
            {
                //string Month = "";
                //string Year = "";
                retdf.DataSource = DataTablelist();
                retdf.DataBind();
            }
            //处罚表
            public DataTable tablecf()
            {
                SqlDataAdapter sda = new SqlDataAdapter(sqlcf, conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dt.Dispose();
                return dt;
            }
            //赔补偿表
            public DataTable tablepbc()
            {
                SqlDataAdapter sda = new SqlDataAdapter(sqlpbc, conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dt.Dispose();
                return dt;
            }
            //强制表
            public DataTable tableqz()
            {
                SqlDataAdapter sda = new SqlDataAdapter(sqlqz, conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dt.Dispose();
                return dt;
            }
            //许可表
            public DataTable tablexk()
            {
                SqlDataAdapter sda = new SqlDataAdapter(sqlxk, conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dt.Dispose();
                return dt;
            }
            //三个相同的表集合在一个表中
            public DataTable table3list()
            {
                DataTable cf = (DataTable)tablecf();

                DataTable newDataTable = cf.Clone();
                object[] obj = new object[newDataTable.Columns.Count];
                //添加处罚表的数据     
                for (int i = 0; i < cf.Rows.Count; i++)
                {
                    cf.Rows[i].ItemArray.CopyTo(obj, 0);
                    newDataTable.Rows.Add(obj);
                }
                //添加赔补偿表的数据 
                DataTable  pbc = (DataTable)tablepbc();
                for (int i = 0; i < pbc.Rows.Count; i++)
                {
                    pbc.Rows[i].ItemArray.CopyTo(obj, 0);
                    newDataTable.Rows.Add(obj);
                }

                //添加强制表的数据  
                DataTable qz = (DataTable)tableqz();
                for (int i = 0; i < qz.Rows.Count; i++)
                {
                    qz.Rows[i].ItemArray.CopyTo(obj, 0);
                    newDataTable.Rows.Add(obj);
                }
                return newDataTable;
            }
            /// <summary>
            /// 将两个列不同(结构不同)的DataTable合并成一个新的DataTable
            /// </summary>
            /// <param name="DataTable1">表1</param>
            /// <param name="DataTable2">表2</param>
            /// <param name="DTName">合并后新的表名</param>
            /// <returns>合并后的新表</returns>
            public DataTable DataTablelist()
            {
                string DTName = "";
                DataTable list3 = (DataTable)table3list();
                DataTable xk = (DataTable)tablexk();

                //克隆DataTable1的结构          
                DataTable newDataTable = list3.Clone();
                for (int i = 0; i < xk.Columns.Count; i++)
                {
                    //再向新表中加入DataTable2的列结构   
                    newDataTable.Columns.Add(xk.Columns[i].ColumnName);
                }
                object[] obj = new object[newDataTable.Columns.Count];
                //添加DataTable1的数据    
                for (int i = 0; i < list3.Rows.Count; i++)
                {
                    list3.Rows[i].ItemArray.CopyTo(obj, 0);
                    newDataTable.Rows.Add(obj);
                }
                if (list3.Rows.Count >= xk.Rows.Count)
                {
                    for (int i = 0; i < xk.Rows.Count; i++)
                    {
                        for (int j = 0; j < xk.Columns.Count; j++)
                        {
                            newDataTable.Rows[i][j + list3.Columns.Count] = xk.Rows[i][j].ToString();
                        }
                    }
                }
                else
                {
                    DataRow dr3;
                    //向新表中添加多出的几行     
                    for (int i = 0; i < xk.Rows.Count - list3.Rows.Count; i++)
                    {
                        dr3 = newDataTable.NewRow();
                        newDataTable.Rows.Add(dr3);
                    }
                    for (int i = 0; i < xk.Rows.Count; i++)
                    {
                        for (int j = 0; j < xk.Columns.Count; j++)
                        {
                            newDataTable.Rows[i][j + list3.Columns.Count] = xk.Rows[i][j].ToString();
                        }
                    }
                }
                newDataTable.TableName = DTName; //设置DT的名字  
                return newDataTable;
            }

    }

    }

  • 相关阅读:
    大数据技术之_04_Hadoop学习_02_HDFS_DataNode(面试开发重点)+HDFS 2.X新特性
    大数据技术之_04_Hadoop学习_01_HDFS_HDFS概述+HDFS的Shell操作(开发重点)+HDFS客户端操作(开发重点)+HDFS的数据流(面试重点)+NameNode和SecondaryNameNode(面试开发重点)
    请不要重复犯我在学习Python和Linux系统上的错误
    教你如何在Kali Linux 环境下设置蜜罐?
    Docker 基础技术:Linux Namespace(下)
    真有用?Snap和Flatpak 通吃所有发行版的打包方式。
    安装Fedora 24后必要的设置
    爆料喽!!!开源日志库Logger的剖析分析
    简单易懂的crontab设置工具集
    新手必看,老鸟绕道–LAMP简易安装
  • 原文地址:https://www.cnblogs.com/luoqin520/p/4763786.html
Copyright © 2020-2023  润新知