Link to home
Start Free TrialLog in
Avatar of jazjef
jazjef

asked on

How does one switch between several SQLDataSource controls and bind them to the same gridview control?

Using:  Visual Web Developer 2008; VB.Net backcoding; SQL Server Express

I have 4 SqlDataSource controls. I have 4 gridviews---each assigned to one of the datasource controls. I can make the appropriate gridviews visible not-visible and set their datasources via a selection from a drop down box control and an autopostback.  This works great----but its really clumsy and has a lot of code.

How can I use just 1 gridview control and specify the datasource to bind to the single gridview control upon the autopostback of the drop down box control? This method should have less code and be more efficient. I think I had done this at some time in the past but I can't remember how I did it. Thanks
Avatar of urir10
urir10

Have the drop down list with AutoPostback enabled.
Then add that to the Page load inside an if statement or something.

GridView.DataSource = yourdatasource

And then once the correct datasource was selected you can bind the gridview to it:

GridView.DataBind()
In your dropdownlist SelectedIndexChanged event you can problematically assign the DataSource for your GridView. So depending on which item is selected in your DropDownList you set the appropriate data source and then DataBind it.


gridView.DataSource = dataSource1
gridView.DataBind()
ASKER CERTIFIED SOLUTION
Avatar of urir10
urir10

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial