zoukankan      html  css  js  c++  java
  • DataTable比较当前行和下行是否一致 DataTable复制结构和复制数据

    DataView dvNeed = dtView.DefaultView;
    dvNeed.Sort = "EMPLOYEEID"; //排序用于下面当前行和下行比较 一般用来添加一行汇总行
    DataTable dtNeed = dvNeed.ToTable(); //排序后的DataTable
    DataTable dtFinally = dtNeed.Clone(); //复制dtNeed的结构 不复制数据 ,复制结构与数据用Copy()
    
    for (int n = 0; n < dtNeed.Rows.Count; n++)
    {
    	string EMPLOYEEID = dtNeed.Rows[n]["EMPLOYEEID"].ToString();
    	string COVERID = dtNeed.Rows[n]["COVERID"].ToString();
    	//判断是否最末一行
    	string CompareEMPLOYEEID = (n == dtNeed.Rows.Count - 1) ? "000000" : dtNeed.Rows[n + 1]["EMPLOYEEID"].ToString();
    	if (CompareEMPLOYEEID != EMPLOYEEID) //比较当前行与下行ID 不同ID 则添加汇总行
    	{
    		#region 
    		DataRow drCount = dtFinally.NewRow();
    		//可以做相关统计信息
    		dtFinally.Rows.Add(drCount);
    		#endregion
    	}
    }
    
    //添加dtNeed中的数据到dtFinally
    foreach (DataRow row in dtNeed.Rows)
    {
    	dtFinally.Rows.Add(row.ItemArray);
    }
    //直接用微软提供的 DataTable.Merge实现
    DataTable DataTable1 = new DataTable();
    DataTable DataTable2 = new DataTable();
    DataTable2.Merge(DataTable1);
    
    //两个结构一样的DataTable合并
    DataTable DataTable1 = new DataTable();   
    DataTable DataTable2 = new DataTable();   
    DataTable newDataTable = DataTable1.Clone();   
      
    object[] obj = new object[newDataTable.Columns.Count];   
    for (int i = 0; i < DataTable1.Rows.Count; i++)   
    {   
        DataTable1.Rows[i].ItemArray.CopyTo(obj,0);   
        newDataTable.Rows.Add(obj);   
    }   
    for (int i = 0; i < DataTable2.Rows.Count; i++)   
    {   
        DataTable2.Rows[i].ItemArray.CopyTo(obj,0);   
        newDataTable.Rows.Add(obj);   
    }  
    //或者  
    DataTable DataTable1 = new DataTable();   
    DataTable DataTable2 = new DataTable();   
      
    object[] obj = new object[DataTable1 .Columns.Count];   
    for (int i = 0; i < DataTable2.Rows.Count; i++)   
    {   
        DataTable2.Rows[i].ItemArray.CopyTo(obj,0);   
        DataTable1.Rows.Add(obj);   
    }  
    
    
    //两个结构不同的DataTable合并 
    /// <summary>   
    /// 将两个列不同的DataTable合并成一个新的DataTable   
    /// </summary>   
    /// <param name="dt1">Table表1</param>   
    /// <param name="dt2">Table表2</param>   
    /// <param name="DTName">合并后新的表名</param>   
    /// <returns></returns>  
      
    private DataTable UniteDataTable( DataTable dt1 ,DataTable dt2 ,string DTName)   
    {    
    	DataTable dt3 = dt1.Clone();   
    	for( int i = 0 ;i < dt2.Columns.Count ;i ++ )   
    	{   
    		dt3.Columns.Add( dt2.Columns[i].ColumnName ) ;   
    	}   
    	object[] obj = new object[dt3.Columns.Count];   
    
    	for (int i = 0; i < dt1.Rows.Count; i++)   
    	{   
    		dt1.Rows[i].ItemArray.CopyTo(obj,0);   
    		dt3.Rows.Add(obj);   
    	}   
    	   
    	if( dt1.Rows.Count >= dt2.Rows.Count )   
    	{   
    		for( int i = 0 ;i < dt2.Rows.Count ;i++ )   
    		{   
    			for( int j = 0 ;j < dt2.Columns.Count ;j ++ )   
    			{   
    			dt3.Rows[i][j+dt1.Columns.Count] = dt2.Rows[i][j].ToString() ;   
    			}   
    		}   
    	}   
    	else   
    	{   
    		DataRow dr3 ;   
    		for( int i = 0 ;i < dt2.Rows.Count - dt1.Rows.Count ;i ++ )   
    		{   
    			dr3 = dt3.NewRow() ;   
    			dt3.Rows.Add( dr3 ) ;   
    		}   
    		for( int i = 0 ;i < dt2.Rows.Count ;i++ )   
    		{   
    			for( int j = 0 ;j < dt2.Columns.Count ;j ++ )   
    			{   
    			dt3.Rows[i][j+dt1.Columns.Count] = dt2.Rows[i][j].ToString() ;   
    			}   
    		}   
    	}   
    	dt3.TableName = DTName ;   
    	return dt3 ;   
    }   


  • 相关阅读:
    <a>标签实现锚点跳跃,<a>标签实现href不跳跃另外加事件(ref传参)
    ThinkPHP实现事务回滚示例代码(附加:PDO的事务处理)
    python 命令执行文件传递参数
    python os.walk()
    python sys.stdin、sys.stdout和sys.stderr
    Python 为什么sys.stdout.write 输出时后面总跟一个数字
    python 不同集合上元素的迭代 chain()
    Python zip() 处理多于两个序列的参数, 存储结对的值
    Python 成对处理数据 zip()
    python 同时迭代多个序列
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234412.html
Copyright © 2011-2022 走看看