How to create datatable from sqldatasource and assign to gridview control? RRS feed

  • Question

  • User969303615 posted

    Hi Everyone,

    Right now I am using the SQLDatasource control to call a stored procedure on an AS400. The SQLDatasource is binded to a gridview control that shows the results. All of this works fine. But what I really want to do is create a datatable from the SQLDatasource after it is databound and assign the datatable to the gridview control's datasource property. I have tried several ways and cannot get it right.

    Can someone show me source on how to do this from creating the datatable from the SQLDatasource control and then assigning it to the gridview? The reason I want to do this is I want to use the datatable for other stuff without requerying such as exporting to excel and other.

    I appreciate your help!


    Monday, April 18, 2011 3:23 PM


All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 18, 2011 3:47 PM
  • User3866881 posted


    As far as I know, SqlDataSource will do nearly everything to fetch data from real db and bind to the GridView automatically, so you needn't do manually.

    However, if you insist, I think you can call something like this:

    GridView1.DataSource = SqlDataSource1.Select(DataSourceSelectArguments.Empty);


    Compared with the first one, I think you can use SqlDataAdapter with DataTable/DataSet, as well as SqlCommandBuilder, which makes your work more flexible when doing binding.


    Wednesday, April 20, 2011 11:17 PM