Solved

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

Posted on 2009-05-08
3
207 Views
Last Modified: 2013-11-27
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
0
Comment
Question by:jazjef
  • 2
3 Comments
 
LVL 7

Expert Comment

by:urir10
ID: 24337436
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()
0
 
LVL 3

Expert Comment

by:xbrady
ID: 24337471
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()
0
 
LVL 7

Accepted Solution

by:
urir10 earned 500 total points
ID: 24337513
Well you will have to have an If statment in the page_load event :

So lets say:
If DropDownList.SelectedIndex = 1 Then
     gridView.DataSource = dataSource1
     gridView.DataBind()
ElseIf DropDownList.SelectedIndex = 1 Then
        gridView.DataSource = dataSource1
        gridView.DataBind()
and so on....


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now