Solved

Dynamic DataSource

Posted on 2006-11-10
16
514 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
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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

24 Experts available now in Live!

Get 1:1 Help Now