Solved

Dynamic DataSource

Posted on 2006-11-10
16
518 Views
Last Modified: 2008-02-01
Hi,
   I am trying to create a DataGridView that is bound to my database.  I've added a DataSource and, when I bind to it, the data displays properly.  However, I would like to have this DataSource change depending on what item is selected in a neighboring TreeView.  So, if I select item1 in the TreeView, the DataGridView's DataSource is filtered on the value of item1.  If I select item2, the DataSource is filtered on the value of item2.  I know how to do this programatically, just wondering if there is a way to do it via VS.NET drag-and-drop.
0
Comment
Question by:DrLondes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
16 Comments
 
LVL 24

Accepted Solution

by:
Justin_W earned 500 total points
ID: 17916581
> I know how to do this programatically, just wondering if there is a
> way to do it via VS.NET drag-and-drop.
Yes and no.

You can't really do it with no coding at all. However, if this is something that you will have to do often, then you could create a custom DataSource Parameter control (coding it only once), and then use that control to do what you described above with no further coding thereafter.

See these links for more info on custom DataSource Parameter controls:
http://weblogs.asp.net/scottgu/archive/2006/01/23/436276.aspx
http://aspnet.4guysfromrolla.com/articles/110106-1.aspx
0
 

Author Comment

by:DrLondes
ID: 17916762
So, create a custom Parameter control, bind that to my TreeView, and pass the parameter to my DataSource?
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17917061
You would include the Parameter's tag inside the DataSource's tag, and that would cause the Parameter's value (which it would retrieve using your custom code) to be passed automatically to the DataSource.

That way, you don't have to write any code when you use the parameter, you just tell the DataSource to use that parameter, and the parameter does the rest itself.
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

Author Comment

by:DrLondes
ID: 17917255
What happens when the TreeView selection is changed?  I would have to manually rebind the DataGridView, no?  I was hoping there was some way to have this all taken care of automatically through databinding.  For instance, I'm pretty sure I've seen code where a dropdown is bound to a particular table and whenever the dropdown selection is changed, a datagrid on the page automatically updates according to the value in the dropdown.  Can't this be done?  Something to do with the BindingContext perhaps?
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17917281
That os because in those cases the DataSource uses a built-in Parameter that monitors the DropDown's selection. That is essentially what you would be doing with the TreeView.
0
 

Author Comment

by:DrLondes
ID: 17917327
Ok, why can't I do that with TreeView then?

Just so I can put this all into perspective then, can you either explain or send me a link about how to do this with a DropDown instead of a TreeView?

Thanks for your help with all this.
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17917384
Check out this link:
http://msdn2.microsoft.com/en-US/library/xt50s8kz.aspx

After reviewing it, the "ControlParameter" Parameter might work for you because it does work with TreeViews, but it will depend on exactly what you are doing with it on your page.
0
 

Author Comment

by:DrLondes
ID: 17917499
I should note, I am using WinForms, not using ASP.NET.
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17917572
In that case, I don't think you can do it via drag-and-drop, unless you create either a custom DataSource or a custom TreeView, and put some code in there. The principal would then be similar to the creation of a Parameter control, but built into either the custom DataSource or custom TreeView.
0
 

Author Comment

by:DrLondes
ID: 17917632
"That os because in those cases the DataSource uses a built-in Parameter that monitors the DropDown's selection."

Can you point me to an example?
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17917806
I was refering to ASP.NET DropDowns, but see the example section of this page:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.controlparameter.aspx
0
 

Author Comment

by:DrLondes
ID: 17917940
So then there is no drag-drop way to set up a WinForm where a databound dropdown determines the data that is displayed in a datagrid?  What is the key difference between ASP.NET and WinForms in this case?
0
 

Author Comment

by:DrLondes
ID: 17917993
http://www.akadia.com/services/dotnet_databinding.html

The "Automatically synchronized Master-Detail" example looks similar to what I want.  I just don't see how the datagrid is tied to the combobox.  How does the datagrid know to update when the selectedindex is changed?
0
 

Author Comment

by:DrLondes
ID: 17918779
This article is very informative and, I think, more along the lines of what I want to do.

http://msdn.microsoft.com/msdnmag/issues/02/02/cutting/
0
 

Author Comment

by:DrLondes
ID: 17921755
Actually, I do want to go with you suggestion.  I've decided to go with a listbox instead of a treeview.  Although this lets me bind to the database and set up a master/detail relationship with a dataGridView, I want to be able to select multiple rows in the ListBox and have the DataGrid display filtered on those rows.  The custom parameter seems perfect for this because I can create a comma-separated list of IDs in the ListBox and use it as a Values list in the DataGridView's Select.  Not as automagic as Mater/Details, but far more flexible.  Thanks!
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17931217
You're welcome.
0

Featured Post

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

626 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