Wie lösen Unable to cast object of type 'System.Daten.DataView' type 'System.Daten.DataTable' FEHLER
habe ich Devxpress GridControl auf die form,
und ich möchte zum senden von Daten an dieses raster nach excel.
und ich will nicht, dies zu tun mit ExportToExcel Methode
ich habe gegoogelt und fand diesen code
aber dieser code ist für das DataGrid-Steuerelement .Net
und es gibt einen Fehler, wenn er versucht zu konvertieren
DevExpress.XtraGrid.Blick.Grid.GridView-System.Daten.DataView
hier ist der code
public string LastCoulmLetter(int coulmnCount)
{
string finalColLetter = string.Empty;
string colCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int colCharsetLen = colCharset.Length;
if (coulmnCount > colCharsetLen)
{
finalColLetter = colCharset.Substring(
(coulmnCount - 1) / colCharsetLen - 1, 1);
}
finalColLetter += colCharset.Substring(
(coulmnCount - 1) % colCharsetLen, 1);
return finalColLetter;
}
public void FromGridToExcel()
{
if (gridView1.RowCount <= 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 < dt.Columns.Count; col++)
{
rawData[0, col] = dt.Columns[col].ColumnName;
}
//Copy the values to the object array
for (int col = 0; col < dt.Columns.Count; col++)
{
for (int row = 0; row < 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();
}
Also was ist das problem und wie man es beheben
InformationsquelleAutor namco | 2012-01-24
Du musst angemeldet sein, um einen Kommentar abzugeben.
Das problem scheint zu sein, dass Ihre
DataSource
ist einDataView
, nicht einDataTable
.Einige Optionen:
Werfe es ein
DataView
wenn Sie möchten, verwenden Sie die gleichen Filter:Verwenden Sie die
.Table
- Eigenschaft, um den Quell-Tabelle, wenn Sie möchten, dass die raw-Daten:InformationsquelleAutor D Stanley
Versuchen Sie dies:
InformationsquelleAutor Iain Ward