Getting Data from a GridView in DataSource

Please note that this blog is only applicable for ASP.NET GridView and in case you are using a Grid froma Third Party source then there might be a different approach to do the same.
 
We have been using the below code to bind a GridView: 
  1. DataTable dt = new DataTable();  
  2.   
  3. GridView1.DataSource =dt;  
  4. GridView1.DataBind();  
 Now, in order to get the data from the already binded GridView, we can use the below code: 
  1. DataTable dt = GridView1.DataSource as DataTable;  
 This will provide the data(including column names) as DataTable in it.
 
Hope it helped
Thanks
Vipul