How to get the datasource of a gridview

Geeth picture Geeth · Jan 6, 2010 · Viewed 9.4k times · Source

I am using a gridview with sqldatasource. How to get back the datasource in the codebehind as a datatable?

Answer

Ravia picture Ravia · Jan 6, 2010

Use System.Data.DataTable dt = (System.Data.DataTable)gview.DataSource; if you are binding a DataTable.

You can even extract the DataTable out of DataSet if you are binding DataSet as System.Data.DataTable dt2 = (System.Data.DataTable)((System.Data.DataSet)gvValidDA.DataSource).Tables[0]; you will have to check the index of your table or table name as you prefer.

Happy coding.