dataGrid转换dataTable

时间:2024-01-16 22:43:08

#region dataGrid转换dataTable
   /// <summary>
   /// dataGrid转换dataTable
   /// </summary>
   /// <param name="dg">dataGrid</param>
   /// <returns>返回dataTable</returns>
   public DataTable DT(DataGrid dg)
   {
    try
    {
     DataTable dt = null;  
       
     if(dg.DataSource is DataView)  
     {  
      dt = (dg.DataSource as DataView).Table;  
     }  
     else if(dg.DataSource is DataTable)  
     {  
      dt = dg.DataSource as DataTable;  
     }  
     else if(dg.DataSource is DataSet)  
     {  
      dt = (dg.DataSource as DataSet).Tables[0]; 
     }
     return dt;
    }
    catch (System.Exception ex)
    {
     throw ex;
    }
   }
   #endregion