Solved

Dynamic DataSource

Posted on 2006-11-10
16
512 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
  • 9
  • 7
16 Comments
 
LVL 24

Accepted Solution

by:
Justin_W earned 500 total points
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:DrLondes
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I should note, I am using WinForms, not using ASP.NET.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 24

Expert Comment

by:Justin_W
Comment Utility
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
Comment Utility
"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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You're welcome.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

12 Experts available now in Live!

Get 1:1 Help Now