C# 如何解决 Unable to cast object of type 'System.Data.DataView' to type 'System.Data.DataTable' ERROR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8988259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve Unable to cast object of type 'System.Data.DataView' to type 'System.Data.DataTable' ERROR
提问by namco
i have Devxpress GridControl on the form,
and i want to send data on this grid to excel.
and i dont want to do this with ExportToExcel method
i have googled and found this code
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
here is the code
我在表单上有 Devxpress GridControl,
我想将此网格上的数据发送到 excel。
我不想用 ExportToExcel 方法来做这个,
我用谷歌搜索并找到了这个代码,
但这个代码是用于 .Net 的 DataGrid 控制的,当它尝试将 DevExpress.XtraGrid.Views.Grid.GridView 转换为 System.Data 时它会出错.DataView
这里是代码
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();
}
So what is the problem and how to fix it
那么问题是什么以及如何解决它
采纳答案by D Stanley
The problem appears to be that your DataSourceis a DataView, not a DataTable.
问题似乎是您的DataSource是DataView,而不是DataTable。
Some options:
一些选项:
Cast it to a DataViewif you want to use the same filters:
DataView如果您想使用相同的过滤器,请将其转换为 a :
DataView dv = (DataView)gridView1.DataSource;
Use the .Tableproperty to get the source table if you want the raw data:
.Table如果需要原始数据,请使用该属性获取源表:
DataTable dt = ((DataView)gridView1.DataSource).Table;
回答by Iain Ward
Try this instead:
试试这个:
DataTable dt = ((DataView)gridView1.DataSource).Table;

