print form

  • Thread starter Thread starter John
  • Start date Start date
J

John

If I want to set up my Vb 2005 windows app so users can print some data from
a dataset, what options do I have to do this? It seems I should be able to
find a whole section on printing data but I haven't been able to find my way
around in "Help" to well.
 
Hi John,

Thank you for posting.

If you want to print some data from a dataset, I think you have two options
to do this.

1. Using crystal report

a. Create a crystal report (.rpt file) in your project.
b. Bind the dataset to the report.
c. If you want to preview the report before printing, add a
CrystalReportViewer control on a form and bind the report to the
CrystalReportViewer.
d. Make use of the CrystalReportViewer by clicking the print button on
its toolbar to print the report. You may also call the PrintToPrinter
method of the report to print it.

For more information on how to create a crystal report, you could refer to
the following link:
http://msdn2.microsoft.com/en-us/library/ms227360(d=ide).aspx

2. Using report definition (.rdlc) file

a. Create a report definition file in your project.
b. Bind the dataset to the report.
c. Add a ReportViewer control on a form and bind the report to the
ReportViewer.
d. Make use of the ReportViewer by clicking the print button on its
toolbar to print the report.

For more information on how to create a report definition file, you could
refer to the link below:
http://msdn2.microsoft.com/en-us/library/ms252067(d=ide).aspx

Hope this helps.
If you have anything unclear, please feel free to let me know.



Sincerely,
Linda Liu
Microsoft Online Community Support

============================================================================
=============================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.

============================================================================
=============================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I have tried the Crystal reports option following the instructions on the
link you sent. The wizard is very easy and straightforward yet, my report
has nothing in it?? What could I be doing wrong? The table definately has
data in it since it dispays fine on the main form. but when I call my
report form, it's a niclety setup form with nothing in it.
 
Hi John,

Thank you for your quick response.

To display data of the DataSet in the crystal report, you should connect
the DataSet to the report in the report designer.

The following is a walkthrough.

1. Double-click the crystal report in Solution Explorer to display it in
the report designer.

2. If Field Explorer doesn't appear, make the Toggle Field View ToolItem
pressed down in the Crystal Reports-Main Toolbar.

3. In Field Explorer, right-click the Database Fields node and select
Database Experts command.

4. In the Database Expert window, unfold the nodes "Project Data" and
"ADO.NET DataSets" , select DataTables from the DataSet and press the add
button.

5. Press OK button to close the Database Expert window.

6. In Field Explorer, you will see the DataTables you selected just now
have been added under the node Database Fields.

7. Drag the fields of the DataTables from Field Explorer and drop onto the
details section of the report one by one.

When the program is running, the data in the DataSet will be displayed in
the crystal report. Of course, you should fill the data into the DataSet
before the crystal report is shown.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
It appears my problem may be because I fill the Dataset in one form which
calls another form that is going to show the report. What is the proper way
to deal with this situation? I tried making it Public just as a trial but
that didn't seem to fix it.
 
Hi John,

Thank you for your response.

If you fill the DataSet in one form(e.g Form1) and call another form(e.g
Form2) to show the report, you should set the DataSource of the report in
Form2 with the DataSet you have filled in Form1before Form2 is shown.

I recommend you to write a public method in Form2 to set the DataSource of
the report. Remember to call this method in Form1 before you show the Form2.

Here is a sample code for you. The sample requires that you have placed a
CrystalReportViewer control named crystalReportViewer1 on Form2.

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
// CrystalReport1 is the report I have created in the project.
You should replace it with the name of your own CrystalReport.
CrystalReport1 report = new CrystalReport1();
this.crystalReportViewer1.ReportSource = report;
}

// The DataSet1 is the name of the DataSet I have created in the
project. You should replace it with the name of your own DataSet.
public void SetDataSourceToReport(DataSet1 dataset)
{

((CrystalReport1)this.crystalReportViewer1.ReportSource).SetDataSource(datas
et);
}
}

public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.SetDataSourceToReport(this.dataset);
frm.Show();
}
}

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
I'm using VB so this is a little difficult for me to look at but I will try
to decipher it. If you could put an example in vb I'd appreciate it though.
 
Hi John,

Thank you for your response.

Here is the example in VB.NET.

Public Class Form2

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
Dim report As New CrystalReport1
Me.crystalReportViewer1.ReportSource = report
End Sub

Public Sub SetDataSourceToReport(ByRef dataset As DataSet)
CType(Me.CrystalReportViewer1.ReportSource,
CrystalReport1).SetDataSource(dataset)
End Sub
End Class


public class Form1 : Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim frm As Form2 = New Form2
frm.SetDataSourceToReport(Me.dataset)
frm.Show()
End Sub

End Class

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Back
Top