日期:2014-05-19  浏览次数:21040 次

无法将类型为“System.Data.DataTable”的对象强制转换为类型“System.Data.DataSet
dbCmdBuilder   =   new   OleDbCommandBuilder(dbAdapter);
DataSet   ds   =   new   DataSet();
ds   =   (System.Data.DataSet)DataGridView实例.DataSource;
dbAdapter.Update(ds);

提示无法将类型为“System.Data.DataTable”的对象强制转换为类型“System.Data.DataSet”


请问该怎样修改上面的代码?

------解决方案--------------------
dbCmdBuilder = new OleDbCommandBuilder(dbAdapter);
DataSet ds = new DataSet();
ds.Tables.Add((System.Data.DataTable)DataGridView实例.DataSource);
dbAdapter.Update(ds);

------解决方案--------------------
ds.Tables.Add( DataGridView实例.DataSource as DataTable );
dbAdapter.Update(ds);

------解决方案--------------------
DataTable dt = DataGridView.DataSoruce as DataTable;

daAdapter.Update(dt.DateSet);
------解决方案--------------------
哎……这么多牛人……

DataSet dataSet;
DataTable dataTable = gridView.DataSource as DataTable;
if ( dataTable == null )
{
dataSet = gridView.DataSource as DataSet;
if ( dataSet == null )
throw new NotSupportedException();
}

if ( dataSet == null )
{
if ( dataTable.DataSet != null )
dataSet = dataTable.DataSet;
else
{
dataSet = new DataSet();
dataSet.Tables.Add( dataTable );
}
}

dbAdapter.Update( dataSet );
------解决方案--------------------
ds.Tables.Add( DataGridView实例.DataSource);
dbAdapter.Update(ds);
------解决方案--------------------
DataTable dt = DataGridView实例.DataSource as DataTable
ds.Tables.Add( dt.Copy() );
dbAdapter.Update(ds);


------解决方案--------------------
DataTable dt = (System.Data.Datatable)DataGridView.DataSoruce
ds.Tables.Add( dt.Copy() );
dbAdapter.Update(ds);