title: How to solve Unable to cast object of type 'System.Data.DataView' to type 'System.Data.DataTable' ERROR
body: <p>i have Devxpress GridControl on the form,<br>
  and i want to send data on this grid to excel.<br>
  and i dont want to do this with ExportToExcel method<br>
  i have googled and found this code<br>
  but this code is for DataGrid control of .Net
  and it gives an error when it tries to convert 
   DevExpress.XtraGrid.Views.Grid.GridView to System.Data.DataView<br>
  here is the code</p>
  
  <pre><code>public string LastCoulmLetter(int coulmnCount)
  {
          string finalColLetter = string.Empty;
          string colCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
          int colCharsetLen = colCharset.Length;
  
          if (coulmnCount &gt; colCharsetLen)
          {
              finalColLetter = colCharset.Substring(
                  (coulmnCount - 1) / colCharsetLen - 1, 1);
          }
  
          finalColLetter += colCharset.Substring(
                  (coulmnCount - 1) % colCharsetLen, 1);
  
          return finalColLetter;
  }
  
  
  public void FromGridToExcel()
  {
          if (gridView1.RowCount &lt;= 0)
              return;
          Excel.Application xls = new Excel.Application();
          Excel.Workbook wb;
          Excel.Worksheet sheet;
  
          object SalakObje = System.Reflection.Missing.Value;
          wb = xls.Workbooks.Add(SalakObje);
          sheet = (Excel.Worksheet)wb.ActiveSheet;
          sheet.Name = "Result";
          xls.Visible = true;
  
          DataTable dt = (DataTable)gridView1.DataSource; // Error comes in here
  
          // Copy the DataTable to an object array
          object[,] rawData = new object[dt.Rows.Count + 1, dt.Columns.Count];
  
          // Copy the column names to the first row of the object array
          for (int col = 0; col &lt; dt.Columns.Count; col++)
          {
              rawData[0, col] = dt.Columns[col].ColumnName;
          }
  
          // Copy the values to the object array
          for (int col = 0; col &lt; dt.Columns.Count; col++)
          {
              for (int row = 0; row &lt; dt.Rows.Count; row++)
              {
                  rawData[row + 1, col] = dt.Rows[row].ItemArray[col];
              }
          }
  
          // Fast data export to Excel
          string excelRange = string.Format("A1:{0}{1}",LastCoulmLetter(dt.Columns.Count), dt.Rows.Count + 1);
  
          sheet.get_Range(excelRange, Type.Missing).Value2 = rawData;
  
          sheet.get_Range(excelRange).Columns.AutoFit();
  }
  </code></pre>
  
  <p>So what is the problem and how to fix it</p>