report viewer problem RRS feed

  • Question

  • Hy,

    I'm having a problem with my report viewer. I made an application for creating and editing bills. When I debug, first form opens with a Datagrid with all currents bills in my sql table. When I double click on one of those bills, second form opens with details of that exact bill (I made a filter, using SELECT with a bill number). And when i press a report button, third form opens with report viewer on it. The problem is that report viewer shows all bills from the first form, and not only the bill from the second form.

    I wonder, what would be appropriate syntax.  

    Thx for any help.

    • Moved by Harry Zhu Wednesday, April 14, 2010 9:43 AM relating to report viewer control (From:Visual C# General)
    Sunday, April 11, 2010 9:59 PM

All replies

  • Are you sharing the same reportViewer object across the forms?  If so, it may take a bit of code to get reportViewer to switch reports. Here's a comment that I saved in my notebook (but you have to change one thing - you have to get the actual DataSource name for Report2 from Visual Studio menus and plug it into the code below):



     Suppose you have a second report called Report2.RDLC. To do this, proceed as follows.

    Report.RDLC files enter the project as embedded resources.
          //Loop through all embedded-resource names to insure getting FULL name of Report2.RDLC
                string nameOfReport2 = "Report2.RDLC";
                System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
                foreach (string str in asm.GetManifestResourceNames())
                    if (str.ToLower().IndexOf(nameOfReport2.ToLower()) > -1) nameOfReport2 = str;
    The fullName is now stored in nameOfReport2. Now we can load Report2 using code:
                    //Let's assume that Report2 has the same datasource name, which is usually NOT the case
                       //WARNING:This name is case senitive.
        string dataSourceName = reportViewer1.LocalReport.DataSources[0].Name; //assumes the same name, Case Senstive
               this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource());
               this.reportViewer1.LocalReport.ReportEmbeddedResource = nameOfReport2;
               this.reportViewer1.LocalReport.DataSources[0].Name = dataSourceName; //WARNING:This name is case senitive
               this.reportViewer1.LocalReport.DataSources[0].Value = dtCustomers; //datatable

    This assumes you are using a datatable as your source.  I've only done the basics with RV, and that was a long time ago.

    Monday, April 12, 2010 12:39 AM