Solved

Populating a DataTable from a BindingSource

Posted on 2009-07-06
7
965 Views
Last Modified: 2012-08-14
Hi, I'm using VB 2005, WinForms. I have several DataGridViews in my application and I populated them with a BindingSource. That all works well.

I now need to create some reports using ReportViewer, but ReportViewer gets its feed from a DataSet, which in turn contains a DataTable.

What I'd like to do is use my BindingSouce to populate my Report's DataTable.

Something like this (although I know this won't work)...

DataTable1.DataSource = bs   << I know this is not correct, but its what I'd like to do.

Is there a way to do this, or do I need to simply poplate a new DataTable from a DataAdapter, CommandBuilder...etc? (I'd really dislike redoing all that)

Thanks for the help and advice,
Fulano
0
Comment
Question by:Mr_Fulano
[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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 12

Accepted Solution

by:
rgn2121 earned 500 total points
ID: 24791714
I have never used a bindingsource, but you might try something like this if you are sure of what the bindingsource contains...

Dim dt As DataTable = DirectCast(bs.DataSource, System.Data.DataTable)
0
 
LVL 13

Expert Comment

by:drypz
ID: 24791717
Hi! Do it something like this.

DataTable dataTable = bs.DataSource as DataTable;
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24791718
fyi...I did not run through this to see if there would be any issues or if it would even work...
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 13

Expert Comment

by:drypz
ID: 24791721
Sorry didn't seen the other comment.
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 24794560
Well, one thing to keep in mind is that when you create a DataSet and fill it from the DataBase, then the loaded DataSet exists in memory:

    DataSet in memory.

When you create a BindingSource, and set it's DataSource to the DataSet that you have already created and loaded, and is in memory, Then the BindingSource is simply pointing to the existing DataSet that is in memory; it's not creating and using a copy of it.  

    DataSet in memory <- BindingSource


So, you can actually use the DataSet itself as the DataSource for the Report.

0
 

Author Comment

by:Mr_Fulano
ID: 24800311
Hi VBRocks, you bring up a good point. That's one of the reasons I wanted to use my BS rather than fill another DataSet just for the report. I actually create my DataSets and DataTables programmatically. I don't use the Wizard. However, for use with ReportViewer its easier to create a "dummy" DataSet and then add DataTables to it, to then be used with in Report. I needed a way to populate my DataTable with the most recent information available, which in y case is the BS.

Thanks for the comment,
Fulano
0
 

Author Closing Comment

by:Mr_Fulano
ID: 31600477
Very good suggestion. This works well.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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