views:

7869

answers:

3

How can I export GridView.DataSource to datatable or dataset?

A: 

Assuming your DataSource is of type DataTable, you can just do this:

myGridView.DataSource as DataTable
Kon
Thanks you are ok...
Phsika
Then how about you **accept** the answer?!
Michael
+3  A: 

http://www.vbforums.com/showthread.php?t=474895

  //If DataSource of GridView1 is a DataTable
  DataTable dt = (DataTable)GridView1.DataSource;

  //If DataSource of GridView1 is a DataView
  DataView dv = (DataView)GridView1.DataSource;
TheTXI
A: 

Personally I would go with:

DateTable tbl = Gridview1.DataSource as DataTable;

This would allow you to test for null as this results in either DataTable object or null. Casting it as a datable using (DataTable)Gridview1.Datasource would cause a crashing error in case the datasource is actually a DataSet or even some kind of collection.

Supporting Dcoumentation: MSDN Documentation on "as"

Tacoman667