In my Form I have this code to open my report on a click of Button:
private void btnGroupOther_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
LayoutControl lc = new LayoutControl();
lc.Dock = DockStyle.Fill;
DateEdit FirstDate = new DateEdit();
DateEdit LastDate = new DateEdit();
lc.AddItem(Resources.firstdate, FirstDate).TextVisible = true;
lc.AddItem(Resources.seconddate, LastDate).TextVisible = true;
lc.Height = 70;
this.Controls.Add(lc);
this.Dock = DockStyle.Top;
if (DevExpress.XtraEditors.XtraDialog.Show(lc, " ", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
RepProductionGroupOther report = new RepProductionGroupOther();
report.DataSource = paint.RepProductionGroupOther(Convert.ToDateTime(FirstDate.EditValue).ToString("MM/dd/yyyy"),
Convert.ToDateTime(LastDate.EditValue).ToString("MM/dd/yyyy"));
report.ShowRibbonPreviewDialog();
}
}
In my header report I have two xrLabel; the first one txtFirstDate and the second one txtLastDate. I want to show the value of FirstDate DateEdit control in txtFirstDate and the value of LastDate DateEdit control in txtLastDate.
How can I do that, the DataSource of the report is sql stored procedure.
It has two Parameters: #FirstDate and #LastDate.
Thanks in advance
I suggest you to go through XtraReport documentation:
Request and Pass Report Parameter Values
private void button1_Click(object sender, EventArgs e) {
// Create a report instance.
XtraReport1 report = new XtraReport1();
// Obtain a parameter and set its value.
report.Parameters["parameter1"].Value = 30;
// Hide the Parameters' UI from end-users (if you did not hide it at design time).
report.Parameters["parameter1"].Visible = false;
// Show the report's print preview depending on your target platform.
// ...
}
Check the "Custom Parameter Editors" section in above documentation
Custom editor implementation for parameters varies depending on your application's platform:
Windows Forms
In Windows Forms applications, you can provide custom parameter editors in the XtraReport.ParametersRequestBeforeShow event handler.
For a code sample, see Provide Custom Editors for Report Parameters.
Below are similar implementation as you are trying to do.. Check these, hope this will help you resolve the issue.
How to pass parameters to devexpress XtraReport from combobox
DevExpress XtraReport Setting a DateTime Parameter to Today
Passing parameters to Xtrareports repx file
Related
I have a XtraReport (DevExpress) in the project, I'm trying to pass a value entered in the editbox (form1) to report the RichBox. How can I do this?
I'm not using any dataset, and only an empty report with some richtextbox.
Solution :
In the tab properties , change the value: xrRichText1 Modifiers = Public
XtraReport1 test = new XtraReport1();
test.xrRichText1.Text = "testing";
//Show Preview
using (ReportPrintTool printTool = new ReportPrintTool(test))
{
printTool.ShowRibbonPreviewDialog();
printTool.ShowRibbonPreviewDialog(UserLookAndFeel.Default);
}
I am completely new to reporting in visual studio C#..I tried searching for some tutorials for beginners but i was unsuccessful..I just found code examples that didn't really explain the basics...I wrote some code which complies and runs fine but it DOES NOT SHOW ANYTHING in the report viewer control in Visual Studio 2013..My code is as follows:
// This method is in a class named Customers.
// When the user clicks the first column of the datagrid view(I have placed a button
// in the first column of the datagrid) a new form opens (ReportForm) and i pass
// the DataSet called dsReport to its constructor.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 )
{
DataSet dsReport = new DataSet();
DataTable tbl = dsReport.Tables.Add();
tbl.Columns.Add("CustomerName", typeof(string));
tbl.Columns.Add("CustomerAddress", typeof(string));
tbl.Columns.Add("MaritalStatus", typeof(string));
tbl.Columns.Add("CustomerType", typeof(string));
tbl.Columns.Add("ImagePath", typeof(string));
foreach (Customer cust in customerList)
{
DataRow dr = dsReport.Tables[0].NewRow();
dr["CustomerName"] = cust.Name;
dr["CustomerAddress"] = cust.Address;
dr["MaritalStatus"] = cust.MaritalStatus;
dr["CustomerType"] = cust.CustomerType;
dr["ImagePath"] = cust.ImagePath;
dsReport.Tables[0].Rows.Add(dr);
}
ReportForm report = new ReportForm(dsReport);
report.Show();
}
}
//Following is the code for the ReportForm Class
//I do not get any results in the report viewer
//I just see the message "The source of the report definition has not been specified"
public ReportForm(DataSet dsReport)
{
InitializeComponent();
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(myReportSource);
this.reportViewer1.ProcessingMode = ProcessingMode.Local;
this.reportViewer1.LocalReport.Refresh();
this.reportViewer1.RefreshReport();
}
private void ReportForm_Load(object sender, EventArgs e)
{
this.reportViewer1.RefreshReport();
}
/* Please note that I have run the code in the debugger and the dataset is being
populated properly and so is the reportViewer1.LocalReport..Also I HAVE NOT
Added any datasources to the project AND I HAVE NOT ADDED ANY Report files(.rdl) files
to the Project */
Lastly can anyone PLEASE answer the following questions:
Q1. Do i absolutely have to include a datasource to work with the report
viewer tool??
Q2. Do i have to include a .rdl file in the project to display a report??
Q3. Is the report viewer tool and a .rdl file one in the same or are they
different??
The ReportViewer is a control that knows how to render a report. It just handles the drawing and some other background tasks, it isn't the actual report. The actual report is the .rdl file (Report Definition Language). It contains all the instructions for generating the report, but it doesn't contain the actual data. The DataSource contains the data that the report operates on.
So to answer your questions specifically:
yes (unless your report is completely static and doesn't use any data).
no, but you need get the .rdl to the ReportViewer somehow. If you don't want to include it as a file you can embed it as a resource in your application, or even hard code it as a string. The ReportViewer has a method that accepts a Stream also, so anything that can provide a stream can act as the source for the .rdl.
They are different, as I explained at the very start.
I created a Form and place GroupPanel in that Form now I created XtraReports and I tried to set that XtraReports in to that GroupPanel of that Form. I tried this code but showing error Best Overloaded method has some invalid arguments
GroupPanel1.Controls.Clear();
XtraReport1 report = new XtraReport1 ();
ReportPrintTool tool = new ReportPrintTool(report);
GroupPanel1.Controls.Add(report); // showing error on this line
report.ShowPreview();
This code is working fine for set a Form2 inside that GroupPanel1 of Form1
panelControl1.Controls.Clear();
var myForm = new ListEmployee(id);
myForm.TopLevel = false;
myForm.AutoScroll = true;
myForm.Anchor = panelControl1.Anchor;
panelControl1.Controls.Add(myForm);
myForm.Show();
Help me to solve this. How to set XtraReports into GroupPanel ?
Thanks in avance, Srihari
If you want to show preview for report you need to use DocumentViewer control:
GroupPanel1.Controls.Clear();
var viewer = new DocumentViewer(); //using DevExpress.XtraPrinting.Preview
viewer.Dock = DockStyle.Fill;
GroupPanel1.Controls.Add(viewer);
var report = new XtraReport1();
viewer.DocumentSource = report;
report.CreateDocument();
If you want to show designer for report you neet to use XRDesignPanel control:
GroupPanel1.Controls.Clear();
var designer = new XRDesignPanel(); //using DevExpress.XtraReports.UserDesigner
designer.Dock = DockStyle.Fill;
GroupPanel1.Controls.Add(designer);
var report = new XtraReport1();
designer.OpenReport(report);
GroupPanel1.Controls.Add() takes as argument an instance of an object descended from the Control class. Since the XtraReport class is noct descended from the Control class you cannot add an XtraReport to a GroupPanel or any other element on a winform.
If you only want to show the output of the report in the panel you could export the report to one of the supported formats.
Since you allready use DevExpress XtraReports you could use ExportToRtf() if you have access to the DevExpress RichEditControl.
I have a list of objects that I'm adding to.
private List<Employee> employees = new List<Employee>();
I have a button on my form application to create a new employee object and add it to the list. Prior to clicking it, it displays "Add Hourly Employee". After clicking it, it changes to "Save Hourly Employee". I'm just using a boolean to determine what text to display.
private void addHrlyEmpBtn_Click(object sender, EventArgs e)
{
resetBtn.Enabled = true;
cancelBtn.Enabled = true;
if (!addHourly)
{
resetBtn.Enabled = true;
cancelBtn.Enabled = true;
textBox4.Enabled = false;
textBox4.Text = (employees.Count + 1).ToString();
textBox7.Enabled = false;
addHrlyEmpBtn.Text = "Save Hourly Employee";
}
else if (addHourly)
{
//Grab values, create new object, and add to list.
//Set addHourly to false;
}
//Other stuff
}
I'm trying to display employees.Count + 1 to textBox4, but for some reason it isn't working. No text is being displayed at all. Idealy, I'd like to have the text box disabled but still display the value. And I only want it to display when !addHourly.
Am I doing something wrong?
There's nothing wrong in principal with the code you wrote.
I would strongly suggest giving meaningful names to all of your variables. Names like textBox4 are likely to cause confusion for yourself and future maintainers of the code.
If the value is not changing as you expect, you are most likely not entering that if branch.
Set a breakpoint at
if (!addHourly)
See if addHourly has the value you expect.
Check if addHrlyEmpBtn_Click is being called at all.
if it isn't try associating the Click event with addHrlyEmpBtn_Click method from the designer
or add addHrlyEmpBtn.Click += addHrlyEmpBtn_Click; in the constructor
I am new to Crystal report, application is in ASP.net 3.5 and MySQL 5.1, going to develop report between dates like from date and to date, first page of report is shown good but when i tried to navigate on another page i got error like Missing Parameter Values same error i got in Printing and Export action
Thanks in advance
public partial class BookingStatement : System.Web.UI.Page
{
//DAL is my Data Access Layer Class
//Book is ReportClass
DAL obj = new DAL();
Book bkStmt = new Book();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//crvBooking is Crystal Report Viewer
//reportFill method is to fill Report
reportFill();
crvBooking.EnableViewState = true;
crvBooking.EnableParameterPrompt = false;
}
/* Also try reportFill() out side !IsPostBack but didn't work */
//Check if the parmeters have been shown.
/* if ((ViewState["ParametersShown"] != null) && (ViewState["ParametersShown"].ToString() == "True"))
{
bkStmt.SetParameterValue(0, "20/04/2010");
bkStmt.SetParameterValue(1, "20/04/2010");
}*/
}
protected void crvBooking_navigate(object sender, CrystalDecisions.Web.NavigateEventArgs e)
{
// reportFill();
}
protected void reportFill()
{
//bkStmt.rpt is Report file
//bookingstatment is View
//bkStmt is ReportClass object of Book
string rptPath = "bkStmt.rpt";
string query = "select * from bookingstatment";
crvBooking.RefreshReport();
crvBooking.Height = 600;
crvBooking.Width = 900;
bkStmt.ResourceName = rptPath;
String dtFrm = bkStmt.ParameterFields[0].CurrentValues.ToString();
obj.SetCommandType(CommandType.Text);
obj.CommText = query;
DataTable dtst = obj.GetDataTable();
crvBooking.ParameterFieldInfo.Clear();
ParameterDiscreteValue discretevalue = new ParameterDiscreteValue();
discretevalue.Value = "20/04/2010"; // Assign parameter
ParameterValues values = new ParameterValues();
values.Add(discretevalue);
bkStmt.SetDataSource(dtst);
ViewState["ParametersShown"] = "True";
crvBooking.EnableViewState = true;
bkStmt.DataDefinition.ParameterFields[0].ApplyCurrentValues(values);
bkStmt.DataDefinition.ParameterFields[1].ApplyCurrentValues(values);
crvBooking.ReportSource = bkStmt;
}
}
The problem seems to occur because Crystal Reports does not persist its parameter values in its ViewState when a postback occurs. So when the CrystalReportViewer attempts to load up the ReportClass it used as its ReportSource again, the parameter values are no longer there.
A solution which we've used successfully is to save the ReportClass (i.e. your Crystal Report object) into Session after all its parameter values have been set & then load this into the CrystalReportViewer upon each PostBack in the Page_Init event. An example:
// instantiate the Crystal Report
var report = new DeliveryLabelsSingle();
// set the required parameters
report.DataSourceConnections[0].SetConnection("DBServer", "DatabaseName", "DatabaseUser", "DatabasePassword");
report.SetParameterValue("#Param1", "val1");
report.SetParameterValue("#Param2", "val2");
// set the data source of the viewer
crvLabels.ReportSource = report;
// save the report object in session for postback binding
Session["rptDeliveryLabels"] = report;
Then the Page_Init event for the page looks like the following:
protected void Page_Init(object sender, EventArgs e)
{
if (IsPostBack) {
if (Session["rptDeliveryLabels"] != null) {
// cast the report from object to ReportClass so it can be set as the CrystalReportViewer ReportSource
// (All Crystal Reports inherit from ReportClass, so it serves as an acceptable data type through polymorphism)
crvLabels.ReportSource = (ReportClass)Session["rptDeliveryLabels"];
}
}
}
In this way, we will always set a report object for the viewer, which has already been initialized with the appropriate values.
Something to keep in mind with this approach is that you will potentially fill up your server memory very quickly, especially if you have lots of users generating lots of different reports. So some housekeeping is in order. We've done this through implementing a base class for all our ASP.NET pages that contain a report (and thus this report loading code). In this base class, we set all possible Session variables that are reports to null. Like so:
// class definition for ASP.NET page containing CrystalReportViewer & associated report(s)
public partial class DeliveryLabelPrint : BaseReport
Then the definition for BaseReport is as follows:
public class BaseReport : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
if (!IsPostBack) {
for (var i = 0; i < Session.Count; i++) {
var sv = Session[i];
// if this session variable contains a Crystal Report, destroy it
if (sv is ReportClass) {
sv = null;
}
}
base.OnLoad(e);
}
}
}
In this way, you ensure that any user only ever has one report in memory at any given time.
If memory is a concern, even with this approach, an alternative could be to store the individual variable values in Session & to then instantiate a new report in Page_Init & repopulate it with the saved values before assigning it to CrystalReportViewer.ReportSource. But in our case, with 40 users pulling 50+ different reports on a daily basis, this implementation of storing of the ReportClass object & the accompanying housekeeping, we haven't run into any memory problems since the application went live 3 years ago. I would still suggest doing appropriate load testing & monitoring before pushing this solution to production, as the results may vary depending on the specific implementation.
When I am writing SQL for a Crystal report, the code for the parameters in SQL is like this:
--Date Range
(
(table.datetime >= '{?Start Date}')
and table.datetime < '{?End Date}')
)
--Location
('{?Facility}'= 'All' OR '{?Facility}' = table.location))
Of course you always have the option of programming the parameters straight into Crystal. This approach is not as efficient but sometimes easier.
I had to use explicit ReportDocument type instead of ReportClass because that raised an invalid cast for some reason, but otherwise, works EXACTLY as advertised AFAICT.
viz.
...
...
if (Session["<some identifier>"] != null)
{
switch (Session["<some identifier>"])
{
case ReportClass rc:
crystalReportViewer1.ReportSource = rc;
break;
case ReportDocument rd:
crystalReportViewer1.ReportSource = rd;
break;
default:
return;
}
}