2016-11-17 49 views
1

代碼:顯示徽標在WPF數據庫RDLC報告標題使用參數

private bool _isReportViewerLoaded; 

private void reportViewer_Load(object sender, EventArgs e) 
{ 
    if (!_isReportViewerLoaded) 
    { 
     Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource(); 
     SellMechDataSet dataset = new SellMechDataSet(); 
     dataset.BeginInit(); 
     reportDataSource1.Name = "DataSet2"; 
     //Name of the report dataset in our .RDLC file 
     reportDataSource1.Value = dataset.tblLogo; 
     this.reportViewer.LocalReport.DataSources.Add(reportDataSource1); 
     this.reportViewer.LocalReport.ReportPath = "D:\\17-6-2016\\ShellMechProject\\demo\\Reports\\CustomerList.rdlc "; 
     dataset.EndInit(); 
     //fill data into WpfApplication4DataSet 
     SellMechDataSetTableAdapters.tblLogoTableAdapter accountsTableAdapter = new SellMechDataSetTableAdapters.tblLogoTableAdapter(); 
     accountsTableAdapter.ClearBeforeFill = true; 
     accountsTableAdapter.Fill(dataset.tblLogo); 
     string ImagePath = ""; 

     if (con.State != ConnectionState.Open) 
      con.Open(); 
     { 
      SqlCommand cmd = new SqlCommand("SELECT * FROM tblLogo", con); 
      SqlDataReader dr; 
      dr = cmd.ExecuteReader(); 
      while (dr.Read()) 
      { 
       ImagePath = dr["ImagePath"].ToString(); 

      } 
     } 
     ReportParameter rp = new ReportParameter("Path", ImagePath); 
     rp.Name = "Path"; 
     //rp.Values = ImagePath; 
     reportViewer.LocalReport.SetParameters(new ReportParameter[] { rp }); 
     reportViewer.RefreshReport(); 
     _isReportViewerLoaded = true; 
    } 
} 

設計:

<Window x:Class="demo.Reports.CustomerList" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:col="clr-namespace:System.Collections;assembly=mscorlib" 
     xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms" 
     Icon="/img\F_Logo.png" 
     WindowStyle="None" 
     ShowInTaskbar="False" 
     Title="Customer Report" Height="742.955" Width="1140.164" WindowStartupLocation="CenterOwner" ResizeMode="NoResize" Loaded="Window_Loaded"> 
    <Grid Margin="-1,2,1,-172" Background="#d9f9f7" Height="913" VerticalAlignment="Top"> 
     <Label Content="Customer Report" VerticalAlignment="Top" Height="25" HorizontalContentAlignment="Center" Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}" Margin="-1,-2,0.2,0"></Label> 
     <Button Content="X" Name="btnExit" Grid.Row="0" Height="20" Width="20" FontFamily="Times New Roman" Foreground="Red" FontWeight="Bold" FontSize="18" Margin="1118,0,2,893" RenderTransformOrigin="4.564,0.572" Click="btnExit_Click" /> 
     <Grid Margin="0,23,0,-23"> 
      <WindowsFormsHost Name="windowsFormsHost1"> 
       <rv:ReportViewer x:Name="reportViewer" Load="reportViewer_Load" /> 
      </WindowsFormsHost> 
     </Grid> 
    </Grid> 
</Window> 

我從數據庫中獲取路徑,將其顯示爲圖像控制 但錯誤發生

本地報告處理期間發生錯誤。

如果在wpf report logo中有任何其他選項可用。

回答

1

EnableExternalImages必須設置爲True,否則圖像將不會顯示,也請確保您的圖像路徑有效。同樣在根據常規類別RDLC報告中的圖像控件的屬性,確保您已設置select the image sourceExternal和值內Use this image來表達=Parameters!Path.Value

reportViewer.LocalReport.EnableExternalImages = true; 
1

當添加參數中的ReportViewer,你會得到

本地報告處理期間發生錯誤。

然後總是看看InnerException消息。這可以是任何東西,例如報告中的DataSet名稱需要與CodeBehind中的名稱匹配。

我試過這個問題,並且使用External=Parameters!Path.Value也沒有成功。

所以下面的代碼是一種解決方法。第一張圖片不是表格標題。我不得不刪除表格標題,並在表格上放置一個圖像。我無法管理從一個表讀取兩個不同的DataSet,從第二個DataSet讀取表頭,並將第一個DataSet用於數據行。

在報告中爲Select the image sourceDatabase被選中,併爲Use this field=First(Fields!Image.Value, "DataSet2")中的單個圖像。對於3張圖片的表格,我只是使用該字段。您需要選擇一種MIME類型,但我的經驗是,它並不需要與實際圖像類型相匹配。

enter image description here

class ReportPhoto 
{ 
    public string FileName { get; set; } 
    public byte[] Photo { get; set; } 

    public ReportPhoto() 
    { 
    } 

    public ReportPhoto(string theFilename, FileInfo thePhotoFile) 
    { 
     FileName = theFilename; 
     Photo = File.ReadAllBytes(thePhotoFile.FullName); 
    } 
} 


class HeaderImage 
{ 
    public byte[] Image { get; set; } 

    public HeaderImage() 
    { 
    } 

    public HeaderImage(FileInfo theImageFile) 
    { 
     Image = File.ReadAllBytes(theImageFile.FullName); 
    } 
} 


public partial class MainWindow : Window 
{ 
    private List<ReportPhoto> _ReportPhotos; 
    private List<HeaderImage> _HeaderImages; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     _ReportPhotos = new List<ReportPhoto>(); 
     _HeaderImages = new List<HeaderImage>();  


     _ReportPhotos.Add(new ReportPhoto("homer.bmp", new FileInfo(@"C:\homer.bmp"))); 
     _ReportPhotos.Add(new ReportPhoto("lisa.bmp", new FileInfo(@"C:\lisa.bmp"))); 
     _ReportPhotos.Add(new ReportPhoto("mag.bmp", new FileInfo(@"C:\mag.bmp"))); 
     _HeaderImages.Add(new HeaderImage(new FileInfo(@"C:\TheSimpsons.png"))); 

     FillReport(); 
    } 

    private void FillReport() 
    { 
     _ReportViewer.LocalReport.ReportEmbeddedResource = "ReportViewerTest2.Report1.rdlc"; 
     PermissionSet permissions = new PermissionSet(PermissionState.Unrestricted); 
     _ReportViewer.LocalReport.SetBasePermissionsForSandboxAppDomain(permissions); 

     Microsoft.Reporting.WinForms.ReportDataSource reportDataSource = new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", _ReportPhotos); 
     Microsoft.Reporting.WinForms.ReportDataSource reportDataSource2 = new Microsoft.Reporting.WinForms.ReportDataSource("DataSet2", _HeaderImages); 
     _ReportViewer.LocalReport.DataSources.Clear(); 
     _ReportViewer.LocalReport.EnableExternalImages = true; 
     _ReportViewer.LocalReport.DataSources.Add(reportDataSource); 
     _ReportViewer.LocalReport.DataSources.Add(reportDataSource2); 
     _ReportViewer.LocalReport.Refresh(); 

     _ReportViewer.LocalReport.Refresh(); 
     _ReportViewer.RefreshReport(); 
    } 
} 
+0

我想從數據庫 – rani

+0

圖像與所述數據從數據庫改變到代碼'的ImagePath =博士[ 「的ImagePath」]的ToString();'不應該這個困難。您正在從路徑中加載文件,並使用從數據庫中提供的路徑。在我的例子中,路徑在代碼中被修復。那麼適應這個問題有什麼問題?我的建議是不要爲報告指定路徑。只需將它加載到字節數組並將字節數組分配給報告字段即可。 – MarkusEgle

+0

好吧,我會嘗試... – rani