views:

16

answers:

1

Hello.

I bound excel data with DataGridView controller successfully. Then I try to save the DataGridView contents to xml file. I want to implement as belwo, but throw exception. How can I fix it using DataGridView? (VS2008 used)

// I tried to implement using this style.But throw exception.
DataSet ds = (DataSet)(dataGridView1.DataSource);  

// One online tutorial posted as this style below
DataSet ds = (DataSet)(dataGrid1.DataSource);  
+1  A: 

Using a breakpoint in the debugger, check what class type dataGridView1.DataSource actually is.

For instance, you might find that if you are using a BindingSource class between the grid view and the data set, that the views data source is not a DataSet, but instead a System.Data.DataView which wraps the DataSet. You would then have to use:

DataSet ds = ((DataView)dataGridView1.DataSource).Table.DataSet;
Dr Herbie