تقييم الموضوع :
  • 1 أصوات - بمعدل 5
  • 1
  • 2
  • 3
  • 4
  • 5
كيفية الطباعة في wpf
#1


السلام عليكم
لا أعلم ان الموضوع مستهلك أم جديد لكنه بالنسبة 
جديد جداً بعد إعلان الشاكي لله المسابقة حول اجمل واجهة على Windows Presentation Foundation 
توجهت لنظر في هذه اللغة وجعلت الهدف الذي يهمني امامي فأنا أحب العمل مع قواعد البيانات فقط !!!
لا أجيد صنع الالعاب ولا البرامج المتعلقة بخدمات الويندوز مطلقاً
فقررت ان اعمل واجهة دخول بسيطة مرتبطة بقاعدة بيانات فتيسر الامر ولكن اردت ان اطبع اي بيانات
وهنا مربط الفرس لم اتمكن بشكل مباشر من اضافة اداة عرض التقارير فاحترت وتساءلت هل من الممكن عمل ذلك؟
فسألت Google 
How add reportviewer in WPF?
فكانت الاجابة منهعددة روابط اخذت اقلبها يمنة ويسرةوجدت مثالين ساعداني كثيراً على تحقيق الهدف المنشود
المثال الاول كالتالي " اعجبتني الفكرة فيه كثيرا" ولكنها اتعبتني

his article shows how to add a RDLC report in WPF.

Suppose I have been assigned the task of generating a RDLC report in WPF.



That contains ID, Name, City, and Order Amount.



So I will show you how to generate a RDLC report in WPF.



It is very simple; believe me it is very simple. So let's start.



Step 1



Create a WPF project; I named it "MonsterBusinessInc".



[صورة مرفقة: RDLCWPF1.jpg]





Step 2



Add a WPF Button, the click event of this button will show you the report; also add the click event for this button.



[صورة مرفقة: RDLCWPF2.jpg]

private void btnClickMe_Click(object sender, RoutedEventArgs e)
 {

 }


Step 3



Add a class in the project under the Entitles folder named "Customer.cs".



Write the code like below:



namespace MonsterBusinessInc.Entities

{
  public class Customer
  {
      public int ID { get; set; }
      public string Name { get; set; }
      public string City { get; set; }
      public int OrderAmount { get; set; }

  }
}


Build the project after doing it.



Step 4



Add an .rdlc file named "CustomerReport.rdlc".



[صورة مرفقة: RDLCWPF3.jpg]





Now we have to add the dataset for this report that will be filled at run time when we display the report; i.e. ID, Name, City etc.



[صورة مرفقة: RDLCWPF4.jpg]



Click the DataSet on the Report Data window and the DataSet Properties window will open. Now we must set the properties.



[صورة مرفقة: RDLCWPF5.jpg]



Set the name to "CustomerDataSet". Now click on the "New" Button; the DataSource Window will open.



[صورة مرفقة: RDLCWPF6.jpg]



Select Object and click the "Next" Button.



[صورة مرفقة: RDLCWPF7.jpg]



Now select the Customer Class under Entities that we created earlier and click the "Finish" button.



[صورة مرفقة: RDLCWPF8.jpg]



Now click on the "OK" button and a DataSet will be added for the Customer.rdlc report.



And you can see it in the Report Data Window. 



[صورة مرفقة: RDLCWPF9.jpg]



Now right-click on the blank white area and select the table.



[صورة مرفقة: RDLCWPF10.jpg]



Now you will have a blank table in the surface.



Click in the blank field; set the filed name. You also can edit the header for the field.



[صورة مرفقة: RDLCWPF11.jpg]





[صورة مرفقة: RDLCWPF12.jpg]



Now we have a .rdlc report ready to display records after supplying the datasource.



Step 5



Add a User Control in the "User Control" folder in the project named "ReportViewer.xaml".



[صورة مرفقة: RDLCWPF13.jpg]



This reportViewer user control will contain the RDLC report.



Now add two DLLs as references.



The first one is Microsoft.ReportViewer.WinForms .



[صورة مرفقة: RDLCWPF14.jpg]



The second one is WindowsFormsIntegration.



[صورة مرفقة: RDLCWPF15.jpg]



Now add the following line in your "ReportViewer.xaml":



xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"


and

<WindowsFormsHost>
          <rv:ReportViewer x:Name="reportViewer"RenderingComplete="reportViewer_RenderingComplete" />
      </WindowsFormsHost>


Now the file will look like:



<UserControl x:Class="MonsterBusinessInc.User_Control.ReportViewer"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
          mc:Ignorable="d"
          d:DesignHeight="600" d:DesignWidth="800" Loaded="UserControl_Loaded"
>
  <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
      <WindowsFormsHost>
          <rv:ReportViewer x:Name="reportViewer"RenderingComplete="reportViewer_RenderingComplete" />
      </WindowsFormsHost>
  </Grid>
</UserControl>


On the UserControl_Loaded event write the following code.



We can bind the report with the database table from the SQL Server database but now I have generated a DataTable at run time with the same fields details Customer.cs under the Entities folder.

 

using System.Data;
using Microsoft.Reporting.WinForms;
namespace MonsterBusinessInc.User_Control
{
  /// <summary>
  /// Interaction logic for ReportViewer.xaml
  /// </summary>
  public partial class ReportViewer : UserControl
  {
      public ReportViewer()
      {
          InitializeComponent();
      }

 private void UserControl_Loaded(object sender, RoutedEventArgs e)
      {
          DataTable dt = new DataTable();
          dt.Columns.Add(new DataColumn ("ID",typeof(int)));
          dt.Columns.Add(new DataColumn("Name", typeof(string)));
          dt.Columns.Add(new DataColumn("City", typeof(string)));
          dt.Columns.Add(new DataColumn("OrderAmount", typeof(int)));

          DataRow dr = dt.NewRow();
          dr["ID"] = 1;
          dr["Name"] = "CK Nitin";
          dr["City"] = "New York";
          dr["OrderAmount"] = 100;
          dt.Rows.Add(dr);
                     

          ReportDataSource reportDataSource = new ReportDataSource();
          reportDataSource.Name = "CustomerReport"; // Name of the DataSet we set in .rdlc
          reportDataSource.Value = dt;
          reportViewer.LocalReport.ReportPath = "D:\\MonsterBusinessInc \\Report\\CustomerReport.rdlc"; // Path of the rdlc file
   
          reportViewer.LocalReport.DataSources.Add(reportDataSource);
          reportViewer.RefreshReport();
      }

private void reportViewer_RenderingComplete(object sender, Microsoft.Reporting.WinForms.RenderingCompleteEventArgs e)
      {

      }
  }
}


Step 7



Now add a window named "CustomerReport.xaml".



Write the following XAML for this XAML file:

 

<Window x:Class="Reporting.CustomerReport"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Title="CustomerReport" Height="600" Width="300"
     xmlns:report="clr-namespace:Reporting.User_Control">
  <Grid>
      <report:ReportViewer ></report:ReportViewer>
     
  </Grid>
</Window>

[صورة مرفقة: RDLCWPF3.jpg]



Step 8



Now the last step.



Open the MainWindows.xaml



Write the following code in the button click:



CustomerReport rpt = new CustomerReport();

          rpt.ShowDialog();

private void btnClickMe_Click(object sender, RoutedEventArgs e)
      {
          CustomerReport rpt = new CustomerReport();
          rpt.ShowDialog();
         
      }



Now Build the project and run it.



Here is the output.



OUTPUT



[صورة مرفقة: RDLCWPF17.jpg]




المصدر لمن اراد  http://www.c-sharpcorner.com/UploadFile/...lc-in-wpf/


تيبع

المثال الثاني بسيط باستخدام وسيط

First of all let's have a small discussion on ReportViewer Control.


ReportViewer



ReportViewer is a freely redistributable control that enables embedding reports in applications developed using the .NET Framework. We can easily design the reports with drag-n-drop simply using Report Designer included in Visual Studio 2010.



The benefits that are offered by ReportViewer Control:

  1. The reporting engine built into ReportViewer can perform operations such as filtering, sorting, grouping and aggregation. And hence processes data efficiently


  2. To present data it supports a variety of ways. So that the data can be present as lists, tables, charts and matrices (also known as crosstabs.)


  3. It adds visual appeal. This means we can specify fonts, colors, border styles, background images etc. to make our report visually appealing.


  4. Enables interactivity in reports. We can have collapsible sections, document map, bookmarks, interactive sorting etc. in our report.


  5. Conditional formatting is also supported by report viewer. We can embed expressions in the report to change display style dynamically based on data values.


  6. Supports printing and print preview.


  7. Supports export to Excel, Word and PDF formats.
Generally we follow the usage of the Report Viewer in Windows Form programs. But here we are going to use this in WPF application.


Step by step creation of report using ReportViewer Control in WPF application



1. Very first step is to open a new project of WPF Application project type and name it to "ReportViewerWPF".


[صورة مرفقة: RptWPF1.jpg]


2. Secondly, click on the Toolbox, and drag a "WindowsFormsHost" control onto the design surface for MainWindow.xaml. This also adds the assemblies that are needed by WindowsFormstHost to our project.


[صورة مرفقة: RptWPF2.jpg]


Code



<WindowsFormsHost Height="100" Width="100"Name="windowsFormsHost1"/>



3. Now, in Solution Explorer, right-click on the project and select Add Reference.



[صورة مرفقة: RptWPF3.jpg]



4. In the .NET tab of the Add Reference dialog log, select the Microsoft.ReportViewer.WinForms assembly, and click OK. After adding successfully it shows in the solution explorer pane under the head "Refrences". 



[صورة مرفقة: RptWPF4.jpg]


[صورة مرفقة: RptWPF5.jpg]


5. Also add the highlighted lines to your code:



  <Window x:Class="ReportViewerWPF.MainWindow"

   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

  xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;

            assembly=Microsoft.ReportViewer.WinForms"

   Title="MainWindow" Height="300" Width="400">

    <Grid>

       <WindowsFormsHost Name="windowsFormsHost1">

           <rv:ReportViewer x:Name="_reportViewer"/>

       </WindowsFormsHost>

    </Grid>

  </Window>



This creates an instance of ReportViewer named _reportViewer in the XAML page.



6. Now let's create an RDLC report that you will display in the ReportViewer control. And for this, In Solution Explorer, right-click your project, point to Add, and select New Item.



[صورة مرفقة: RptWPF6.jpg]



7. In the Add New Item dialog box, select Report Wizard which is in Reporting template, and click Add. This will open the Report Wizard with the Data Source Configuration Wizard like this:



[صورة مرفقة: RptWPF7.jpg]


[صورة مرفقة: RptWPF8.jpg]


8. In the Data Source Configuration Wizard, select your Database and click Next, then again select Dataset, and click Next.



[صورة مرفقة: RptWPF9.jpg]


[صورة مرفقة: RptWPF10.jpg]


9. After clicking on Next, Choose Your Data Connection page will be open, click New Connection. If you see the Choose Data Source dialog box, select Microsoft SQL Server and click Continue.



[صورة مرفقة: RptWPF11.jpg]



10. In the Server name box, type the server name that hosts your database that you are going to select, as I have used the localhost and for this I have to log on to my server and in "Select or enter a database" name, select the database which you are going to use, and test your connection before clicking OK.



[صورة مرفقة: RptWPF12.jpg]



11. Click Next button twice, so that the page "Choose your Database Objects" will be opened .



12. In the Choose Your Database Objects page, expand the Tables node, select the checkbox for the table for which you want to create a report, and click Finish.



[صورة مرفقة: RptWPF13.jpg]



13. A DataSet object called YourDatabaseNameDataSet is now created in your project.


[صورة مرفقة: RptWPF14.jpg]


14. In the Report Wizard, click Next.



15. In the Arrange Fields page, drag all the available fields and drop to the Values pane. This creates a simple tabular table for displaying the sample data. Then, click Next two times and lastly click on Finish to close the Report Wizard.



[صورة مرفقة: RptWPF15.jpg]



16. Now your window of report.rdlc will look like as under:



[صورة مرفقة: RptWPF16.jpg]



17. Next, it's time to add the code to point ReportViewer to the new report that we have created and also to add data from "YourDatabaseNameDataSet" to ReportViewer.

18. Open MainWindow.xaml.cs, and add the following lines:



MainWindow.xaml.cs:



namespace ReportViewerWPF

{  

    public partial class MainWindow : Window

    {

       public MainWindow()

        {

            InitializeComponent();

            _reportViewer.Load += ReportViewer_Load;

        }

        private bool _isReportViewerLoaded;


       private void ReportViewer_Load(object sender, EventArgs e)
       {

           if (!_isReportViewerLoaded)

           {

                Microsoft.Reporting.WinForms.ReportDataSourcereportDataSource1 = new      

                Microsoft.Reporting.WinForms.ReportDataSource();

                WpfApplication4DataSet dataset = newWpfApplication4DataSet();


               dataset.BeginInit();

               reportDataSource1.Name = "DataSet1";
               //Name of the report dataset in our .RDLC file


               reportDataSource1.Value = dataset.Accounts;
              this._reportViewer.LocalReport.DataSources.Add(reportDataSource1);


               this._reportViewer.LocalReport.ReportPath ="../../Report.rdlc";                    
               dataset.EndInit();


               //fill data into WpfApplication4DataSet

               WpfApplication4DataSetTableAdapters.AccountsTableAdapter

               accountsTableAdapter = new  

                WpfApplication4DataSetTableAdapters.AccountsTableAdapter();

 

                accountsTableAdapter.ClearBeforeFill = true;

                accountsTableAdapter.Fill(dataset.Accounts);                    

                _reportViewer.RefreshReport();

                _isReportViewerLoaded = true;

            }

        }

    }

}


وهنا المصدر ايضاً
http://www.c-sharpcorner.com/UploadFile/...ol-in-wpf/


Shy  اعلم انه  Copy & Paste
لكني اجعله كمرجع لي أولا ولمن يهمه ثانياً
خصوصا ان الكود بالمثالين سي عفريت
قصدي شارب




سبحان الله والحمدلله ولا إله إلا الله والله أكبر
 رَبِّ اغْفِرْ لِي وَلِوَالِدَيَّ
Heart
الرد }}}}
#2
بارك الله فيك

انت عكسي تماما ، انا لااهتم ابدا بقواعد البيانات
في حياتي ماتعاملت مع الreports او الطباعة Smile
الرد }}}}
تم الشكر بواسطة: أبو عمر
#3
وعليكم السلام ورحمة الله وبركاته
كونك وصلت لحل فليتك تضع لنا درساً يشرح كيفية إضافة التقارير والطباعة في هذه التقنية
أنا موله جداً بقواعد البيانات
Smile
فاعلم أنه لا إله إلا الله
الرد }}}}
تم الشكر بواسطة: أبو عمر
#4
ابشر
خلاصة ذاك الموضوع الطويل اضافة مستضيف للكائن تبعنا وهو اداة عرض التقارير
وتتعامل معها فيما بعد في code behind
ان شاء الله الان اجهز مثال في صحة وحدة واريح دماغي ودماغ المتابع
سبحان الله والحمدلله ولا إله إلا الله والله أكبر
 رَبِّ اغْفِرْ لِي وَلِوَالِدَيَّ
Heart
الرد }}}}
تم الشكر بواسطة: السندبااد


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  WPF مثال تطبيقي على الطباعة أبو عمر 6 431 28-09-15, 04:28 AM
آخر رد: أبو عمر
  الطباعة في WPF يا ليت الادارة تحل الموضوع أبو عمر 3 199 26-09-15, 12:30 PM
آخر رد: السندبااد
  [مقال] كيفية اضافة Theme في WPF Sajad 5 941 04-08-14, 05:48 AM
آخر رد: shwehdi4pc

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم