Results 1 to 13 of 13

Thread: RDLC Report With Two DataSets. How To ?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    22

    Wink RDLC Report With Two DataSets. How To ?

    Hi,
    I'm creating a small vb.net(VS2010) application and it has two datagridviews. I want to print the data on gridviews using reportviewer. So I'm trying to use two datasets. With one dataset I can print the gridview, but I don't understand how to use two datasets with the report. Can someone please help me. This is the code I used.

    First I added a dataset and created a table on it. Then I used this code.

    Dim dt As New DataTable
    With dt
    .Columns.Add("column1")
    .Columns.Add("column2")
    End With


    For Each row As DataGridViewRow In datagridview.Rows
    dt.Rows.Add(row.Cells(2).Value, row.Cells(3).Value)
    Next

    Report.ReportViewer1.LocalReport.DataSources.Item(0).Value = dt
    Report.ShowDialog()
    Report.Dispose()

    How do I use two datasets to print the other gridview on the same report ?

    Thanks!

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: RDLC Report With Two DataSets. How To ?

    When you designed the RDLC, did you add 2 datasets to it?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    22

    Re: RDLC Report With Two DataSets. How To ?

    Quote Originally Posted by kleinma View Post
    When you designed the RDLC, did you add 2 datasets to it?
    Thank you for the reply. I can only add one data set. When I add two data sets to the report , the report doesn't show data. It shows me it cannot find data source for the second data set. I want to use two tablix on the report. A data set for each tablix. Thanks!

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: RDLC Report With Two DataSets. How To ?

    If you set 2 data sources when designing the RDLC file, then when you set your datasource with valid data in this line of code

    Report.ReportViewer1.LocalReport.DataSources.Item(0).Value = dt

    you should also be setting the value of datasources.item(1).value, which would be the second datasource you had added.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    22

    Re: RDLC Report With Two DataSets. How To ?

    Dim dt As New DataTable
    With dt
    .Columns.Add("Column1")
    .Columns.Add("Column2")
    End With


    For Each row As DataGridViewRow In GV2.Rows
    dt.Rows.Add(row.Cells(2).Value, row.Cells(3).Value)
    Next


    Dim dt2 As New DataTable
    With dt2
    .Columns.Add("Column1")
    .Columns.Add("Column2")
    End With


    For Each row As DataGridViewRow In GV4.Rows
    dt2.Rows.Add(row.Cells(2).Value, row.Cells(3).Value)
    Next

    Report.ReportViewer1.LocalReport.DataSources.Item(0).Value = dt
    Report.ReportViewer1.LocalReport.DataSources.Item(1).Value = dt2


    But When I run the project it highlights me the last line and shows me this error

    "Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index"



    Quote Originally Posted by kleinma View Post
    If you set 2 data sources when designing the RDLC file, then when you set your datasource with valid data in this line of code

    Report.ReportViewer1.LocalReport.DataSources.Item(0).Value = dt

    you should also be setting the value of datasources.item(1).value, which would be the second datasource you had added.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: RDLC Report With Two DataSets. How To ?

    I created you a sample project that uses an RDLC file with 2 datasources defined. In the example the datasources are just pointing to simple objects I created (a customer and product class) instead of a database, but the concept is the same. Open it, and build it, and run it to see in action.
    Attached Files Attached Files

  7. #7
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: RDLC Report With Two DataSets. How To ?

    First, your not using any Datasets. You are using a DataTable. I haven't done any RDLC reports in years and you haven't explained what's in the two Datagridviews but it seems the answer is that you DON't need two Datsets, just two DataTables in one Dataset

    Code:
            Dim ds As New DataSet
    
            Dim dt As New DataTable
            With dt
                .Columns.Add("column1")
                .Columns.Add("column2")
            End With
    
            Dim dt2 As New DataTable
            With dt2
                .Columns.Add("column1")
                .Columns.Add("column2")
            End With
    
            'LOAD the DataTables
            '
            '
    
            ds.Tables.Add(dt)
            ds.Tables.Add(dt2)
    
            Report.ReportViewer1.LocalReport.DataSources.Add(ds)
    Now a lot depends on how you created the report. Did you create it using a Type Dataset? You really need to provide more information.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    22

    Re: RDLC Report With Two DataSets. How To ?

    Quote Originally Posted by kleinma View Post
    I created you a sample project that uses an RDLC file with 2 datasources defined. In the example the datasources are just pointing to simple objects I created (a customer and product class) instead of a database, but the concept is the same. Open it, and build it, and run it to see in action.
    kleinma Thank you very much for the example. I see you have added 2 data sets on your rdlc file.
    Name:  dsexample1.jpg
Views: 6822
Size:  34.5 KB

    Can you please tell me how did you add those two datasets. What I use to do is first goto the solution explorer , right click add new item add datasets then go to the rdlc file and add the dataset. I cannot see data sets in your projects solution explorer.

    Thanks!
    Last edited by dumiduw; Aug 14th, 2016 at 03:35 AM.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    22

    Re: RDLC Report With Two DataSets. How To ?

    Here I have attached my project. It has a simple database file and two forms. One form is for datagridviews and other is for the report. It can print one dataset but I don't understand how to print another dataset using another tablix. Can you please check this. Thanks!

    WindowsApplication1.zip

  10. #10
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: RDLC Report With Two DataSets. How To ?

    I took a quick look at your project and it seem your over complicating the problem. Why are you trying to use two datasets? Put all the reports tables in one dataset. Here's a walkthrough
    https://msdn.microsoft.com/en-us/lib...v=nav.90).aspx

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    22

    Re: RDLC Report With Two DataSets. How To ?

    Hi thank you very much for checking it. Actually I prefer to use two tables on one dataset on this, but when I tried, I got so many errors. I'll check your guide. Thanks!

    Quote Originally Posted by wes4dbt View Post
    I took a quick look at your project and it seem your over complicating the problem. Why are you trying to use two datasets? Put all the reports tables in one dataset. Here's a walkthrough
    https://msdn.microsoft.com/en-us/lib...v=nav.90).aspx

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    22

    Re: RDLC Report With Two DataSets. How To ?

    I tested @kleinma's way. Two classes as datasets, two binding sources on the reportviewer form and feeding the binding sources with gridview data in a loop . Its working fine thanks kleinma. Now I'm trying as @wes4dbt advised. two data tables in a dataset. Thank you all for the kind help.

  13. #13
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: RDLC Report With Two DataSets. How To ?

    Sounds good. I am glad you are making progress.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width