locked
How can i cahnge the sqldatasource for my gridview in the server side code (C#) ?? RRS feed

  • Question

  • User-574937131 posted

    I have GridViewSeach and i have 8 sqldatasrouce-s I want to change the datasource dynamically during the program . 

    Is that possible? 

    GridViewSearch.DataSource = SqlDataSource1; is not working ! 

    please help 

    thanks! 

    Saturday, September 10, 2011 3:30 AM

Answers

  • User3866881 posted

    Hello raffi:)

    You cannot offer a SqlDataSource1—— an data source control to GridView (the data presentation control) directly. You should do like this:

    GridView1.DataSourceId="SqlDataSource1";

    And any way, I suggest you trying to use several SqlDataAdapters and Fill data contents into DataSet, thus you can get several tables. And bind these tables to GridView accroding to different kinds of situations. Something like this:

    using (SqlDataAdapter adapter = new SqlDataAdapter("select * from xxx","Your conn str"))

    {

         if(some condition 1)

         {

               adapter.SelectCommand.CommandText="Your new sql string";

         }

         ………………

         DataTable dt = new DataTable();

         adapter.Fill(dt);

         GridView1.DataSource = dt;

         GridView1.DataBind();

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 11, 2011 8:44 PM

All replies

  • User-364289655 posted

    After setting the DataSource, you must bind it, GridViewSearch.DataBind();

    Saturday, September 10, 2011 3:33 AM
  • User-574937131 posted

    Yeah sure I do that , but still not woriking . 

    When im selecting the datasource from the design section before Ctl+F5 its working just fine . but when im diselecting anydatasource and then trying to do it in a submit button its not working ! what could be the problem? 

    Saturday, September 10, 2011 3:40 AM
  • User-364289655 posted

    Can you post relevant code?

    Saturday, September 10, 2011 3:53 AM
  • User3866881 posted

    Hello raffi:)

    You cannot offer a SqlDataSource1—— an data source control to GridView (the data presentation control) directly. You should do like this:

    GridView1.DataSourceId="SqlDataSource1";

    And any way, I suggest you trying to use several SqlDataAdapters and Fill data contents into DataSet, thus you can get several tables. And bind these tables to GridView accroding to different kinds of situations. Something like this:

    using (SqlDataAdapter adapter = new SqlDataAdapter("select * from xxx","Your conn str"))

    {

         if(some condition 1)

         {

               adapter.SelectCommand.CommandText="Your new sql string";

         }

         ………………

         DataTable dt = new DataTable();

         adapter.Fill(dt);

         GridView1.DataSource = dt;

         GridView1.DataBind();

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 11, 2011 8:44 PM
  • User-1835172754 posted

    you could change the connection string name...

    Wednesday, September 14, 2011 12:49 AM