将DataView转化为DataTable方法!

  /// <summary>
  /// 将DataView转化为DataTable
  /// </summary>
  /// <param name="obDataView"></param>
  /// <returns></returns>
  public  DataTable GetDataTable(DataView obDataView)
  {
   if (null == obDataView)
   {
    throw new ArgumentNullException("DataView", "Invalid DataView object specified");
   }

   DataTable obNewDt = obDataView.Table.Clone();
   int idx = 0;
   string [] strColNames = new string[obNewDt.Columns.Count];
   foreach (DataColumn col in obNewDt.Columns)
   {
    strColNames[idx++] = col.ColumnName;
   }

   IEnumerator viewEnumerator = obDataView.GetEnumerator();
   while (viewEnumerator.MoveNext())
   {
    DataRowView drv = (DataRowView)viewEnumerator.Current;
    DataRow dr = obNewDt.NewRow();
    try
    {
    foreach (string strName in strColNames)
    {
    dr[strName] = drv[strName];
    }
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }
    obNewDt.Rows.Add(dr);
   }
   return obNewDt;
  }

posted @ 2005-09-25 02:24  pot  阅读(2605)  评论(1编辑  收藏  举报