资料表工具类(DataTableTools)
作者:C/S框架网  发布日期:2015-01-04 20:02:52
  资料表工具类(DataTableTools)


C# Code:

/// <summary>
/// 资料表工具类
/// </summary>
public sealed class DataTableTools
{
   /// <summary>
   /// 添加合并的列数据
   /// </summary>
   /// <param name="dt">数据表</param>
   /// <param name="mergedColumnName">合并数据后的列名</param>
   /// <param name="fields">将要合并数据的字段名</param>
   public static void AddMergedColumn(DataTable dt, string mergedColumnName, string[] fields)
   {
      if (dt.Columns[mergedColumnName] == null)
      dt.Columns.Add(mergedColumnName, Type.GetType("System.String"));
      string Combine = string.Empty;
      foreach (DataRow dr in dt.Rows)
      {
         Combine = string.Empty;
         foreach (string Col in fields)
         {
            if (String.IsNullOrEmpty(Combine))
            Combine = ConvertEx.ToString(dr[Col]);
            else
            Combine = " - " ConvertEx.ToString(dr[Col]);
         }
         dr[mergedColumnName] = Combine;
      }
   }
   
   /// <summary>
   /// 在资料表的第一行添加一条空行
   /// </summary>
   /// <param name="dt"></param>
   public static void AddFirstEmptyRow(DataTable dt)
   {
      if (dt.Rows.Count == 0) return;
      if (dt.Rows[0].IsNull(0)) return;
      
      //插入一行空记录
      DataRow row = dt.NewRow();
      dt.Rows.InsertAt(row, 0);
   }
   
   /// <summary>
   /// 调换两行之间的位置
   /// </summary>
   /// <param name="index1"></param>
   /// <param name="index2"></param>
   /// <param name="dt"></param>
   /// <returns></returns>
   public static void SwapRow(int index1, int index2, DataTable dt)
   {
      DataRow dr = dt.NewRow();
      DataRow row = dt.Rows[index1];
      dr.ItemArray = row.ItemArray;
      dt.Rows.InsertAt(dr, index2);
      dt.Rows.Remove(row);
      dt.AcceptChanges();
   }
   
}


//来源:C/S框架网(www.csframework.com) QQ:1980854898


上一篇 下一篇