Convert DataView to DataTable

   
   
   
   
  1. public static DataTable CreateTable (DataView obDataView )
  2. {
  3. if ( null == obDataView )
  4. {
  5. throw new ArgumentNullException
  6. ( "DataView", "Invalid DataView object specified" );
  7. }
  8.  
  9. DataTable obNewDt = obDataView. Table. Clone ( );
  10. int idx = 0;
  11. string [ ] strColNames = new string [obNewDt. Columns. Count ];
  12. foreach (DataColumn col in obNewDt. Columns )
  13. {
  14. strColNames [idx++ ] = col. ColumnName;
  15. }
  16.  
  17. IEnumerator viewEnumerator = obDataView. GetEnumerator ( );
  18. while (viewEnumerator. MoveNext ( ) )
  19. {
  20. DataRowView drv = (DataRowView )viewEnumerator. Current;
  21. DataRow dr = obNewDt. NewRow ( );
  22. try
  23. {
  24. foreach ( string strName in strColNames )
  25. {
  26. dr [strName ] = drv [strName ];
  27. }
  28. }
  29. catch (Exception ex )
  30. {
  31. Trace. WriteLine (ex. Message );
  32. }
  33. obNewDt. Rows. Add (dr );
  34. }
  35.  
  36. return obNewDt;
  37. }

 

你可能感兴趣的:(exception,String,object,null)