Solved

Populating a DataTable from a BindingSource

Posted on 2009-07-06
7
958 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 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