我做了两个实体类,我想把mer.GetProducts()中取到的数据放到Report1.rdlc报表中
1.是不是我在xaml文件中要设置一下DataSurce的name啊.我一直找不到解决方法.
2.用实体类的List绑定到数据源的方式我这样做法对吗.
请高手指点.Window1.xaml代码如下
<Window x:Class="WpfApplication2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:viewer="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
    
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">    <DockPanel Name="panel1" LastChildFill="True">
                
        <my:WindowsFormsHost Margin="341,249,237,165" Name="windowsFormsHost1"  
             xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" OverridesDefaultStyle="False">
            <viewer:ReportViewer Name="rp" Width="1000" Height="10000"  >                            </viewer:ReportViewer>
        </my:WindowsFormsHost>
    </DockPanel>     
</Window>Window1.cs代码如下
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //this.ReportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
            //this.ReportViewer1.LocalReport.ReportPath = @"E:\C#\WpfApplication2\WpfApplication2\Report\Report1.rdlc";
            //this.ReportViewer1.LocalReport.Refresh();            Microsoft.Reporting.WinForms.ReportViewer rep = new Microsoft.Reporting.WinForms.ReportViewer();
            rep.LocalReport.ReportPath = @"E:\C#\WpfApplication2\WpfApplication2\Report\Report1.rdlc";
            Merchant mer = new Merchant();
            
            rep.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource([color=#0000FF]"Test" ,mer.GetProducts()));[/color]
            rep.RefreshReport();        }实体类
    public class Product 
    {
        private string m_name;
        private int m_price;
        public Product(string name, int price)
        {
            m_name = name; m_price = price;
        }        public string Name
        {
            get
            {
                return m_name;
            }        }        public int Price
        {
            get
            {
                return m_price;
            }
        }
    }
*********************************************
    public class Merchant
    {
        private List<Product> m_products;
        public Merchant()
        {
            m_products = new List<Product>();
            m_products.Add(new Product("Pen", 25));
            m_products.Add(new Product("Pencil", 30));
            m_products.Add(new Product("Notebook", 15));
        }        public List<Product> GetProducts()
        { 
            return m_products;
        }
    }

解决方案 »

  1.   

    你是用的rdlc还是水晶报表1.根据需求建立一个符合条件的实体的dataset
    2.创建报表文件并绑定实体的dataset
    3.创建一个页面 放置ReportViewer 然后再该页面下面写绑定代码 很简单
      

  2.   

    绑定代码void ReportBinder(int monthId)
            {            DataSet dsGet = CreateTotalDatatable(monthId);            this.ReportViewer1.ProcessingMode = ProcessingMode.Local;            ReportViewer1.LocalReport.ReportPath = Server.MapPath("..") + @"/ReportFiles/rptModelChannel1.rdlc";
                ReportViewer1.LocalReport.DataSources.Clear();            ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dsModelChannel_dtModelChannel", dsGet.Tables[0]));
                ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dsModelChannel_dtWeek", dsGet.Tables[1]));
                ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dsModelChannel_dtModelChannelNonePerson", dsGet.Tables[2]));            ReportParameter rp_CompanyName = new ReportParameter("rp_CompanyName",SessionState.CurrentUser.ShopName);
                ReportParameter rp_LastId;
                if (dsGet.Tables[0].Rows.Count > 0)
                {
                    rp_LastId = new ReportParameter("rp_LastId", dsGet.Tables[0].Rows[dsGet.Tables[0].Rows.Count - 1]["brand_id"].ToString());
                }
                else
                {
                    rp_LastId = new ReportParameter("rp_LastId", "-1");
                }
                ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp_CompanyName, rp_LastId });            ReportViewer1.LocalReport.Refresh();        }
      

  3.   

    下面几行是给报表传参数的 你不需要的话 可以省略掉
     ReportParameter rp_CompanyName = new ReportParameter("rp_CompanyName",SessionState.CurrentUser.ShopName);
                ReportParameter rp_LastId;
                if (dsGet.Tables[0].Rows.Count > 0)
                {
                    rp_LastId = new ReportParameter("rp_LastId", dsGet.Tables[0].Rows[dsGet.Tables[0].Rows.Count - 1]["brand_id"].ToString());
                }
                else
                {
                    rp_LastId = new ReportParameter("rp_LastId", "-1");
                }
                ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp_CompanyName, rp_LastId });
      

  4.   

    我怎么添加不上ReportViewer控件啊