Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamic DataSource

Posted on 2006-11-10
16
Medium Priority
?
520 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 2000 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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