السلام عليكم
لا أعلم ان الموضوع مستهلك أم جديد لكنه بالنسبة
جديد جداً بعد إعلان الشاكي لله المسابقة حول اجمل واجهة على 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".
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.
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".
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.
Click the DataSet on the Report Data window and the DataSet Properties window will open. Now we must set the properties.
Set the name to "CustomerDataSet". Now click on the "New" Button; the DataSource Window will open.
Select Object and click the "Next" Button.
Now select the Customer Class under Entities that we created earlier and click the "Finish" button.
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.
Now right-click on the blank white area and select the table.
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.
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".
This reportViewer user control will contain the RDLC report.
Now add two DLLs as references.
The first one is Microsoft.ReportViewer.WinForms .
The second one is WindowsFormsIntegration.
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>
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
المصدر لمن اراد 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:
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".
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.
Code
<WindowsFormsHost Height="100" Width="100"Name="windowsFormsHost1"/>
3. Now, in Solution Explorer, right-click on the project and select Add Reference.
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".
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.
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:
8. In the Data Source Configuration Wizard, select your Database and click Next, then again select Dataset, and click Next.
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.
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.
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.
13. A DataSet object called YourDatabaseNameDataSet is now created in your project.
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.
16. Now your window of report.rdlc will look like as under:
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/
اعلم انه Copy & Paste
لكني اجعله كمرجع لي أولا ولمن يهمه ثانياً
خصوصا ان الكود بالمثالين سي عفريت
قصدي شارب
لا أعلم ان الموضوع مستهلك أم جديد لكنه بالنسبة
جديد جداً بعد إعلان الشاكي لله المسابقة حول اجمل واجهة على 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".
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.
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".
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.
Click the DataSet on the Report Data window and the DataSet Properties window will open. Now we must set the properties.
Set the name to "CustomerDataSet". Now click on the "New" Button; the DataSource Window will open.
Select Object and click the "Next" Button.
Now select the Customer Class under Entities that we created earlier and click the "Finish" button.
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.
Now right-click on the blank white area and select the table.
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.
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".
This reportViewer user control will contain the RDLC report.
Now add two DLLs as references.
The first one is Microsoft.ReportViewer.WinForms .
The second one is WindowsFormsIntegration.
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>
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
المصدر لمن اراد 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:
- The reporting engine built into ReportViewer can perform operations such as filtering, sorting, grouping and aggregation. And hence processes data efficiently
- 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.)
- It adds visual appeal. This means we can specify fonts, colors, border styles, background images etc. to make our report visually appealing.
- Enables interactivity in reports. We can have collapsible sections, document map, bookmarks, interactive sorting etc. in our report.
- Conditional formatting is also supported by report viewer. We can embed expressions in the report to change display style dynamically based on data values.
- Supports printing and print preview.
- Supports export to Excel, Word and PDF formats.
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".
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.
Code
<WindowsFormsHost Height="100" Width="100"Name="windowsFormsHost1"/>
3. Now, in Solution Explorer, right-click on the project and select Add Reference.
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".
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.
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:
8. In the Data Source Configuration Wizard, select your Database and click Next, then again select Dataset, and click Next.
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.
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.
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.
13. A DataSet object called YourDatabaseNameDataSet is now created in your project.
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.
16. Now your window of report.rdlc will look like as under:
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/
اعلم انه Copy & Paste
لكني اجعله كمرجع لي أولا ولمن يهمه ثانياً
خصوصا ان الكود بالمثالين سي عفريت
قصدي شارب
سبحان الله والحمدلله ولا إله إلا الله والله أكبر
اللهم اغْفِرْ لِلمؤمنين والمؤمنات والمسلمين والمسلمات الأحياء منهم والأموات