Place a ToolstripButton on to the Form :
private void toolStripButton2_Click(object sender, EventArgs e)
{
if (this.ActiveMdiChild == null)
return;
printForm1.Form = this.ActiveMdiChild;
printForm1.PrintAction = System.Drawing.Printing.PrintAction.PrintToPreview;
printForm1.Print();
}
Blog Archive
My Links
- C# Snippets: 1
- C# Snippets: 2
- XMLFOX - CSHARP SAMPLES
- Q: Export your Crystal Report data to PDF
- Q: To create a Excel report wihtout using Excel object model Ex
- Q: Using C#, How to Save,Update Data to and from XML with out using the sql server database
- Q: upload excel to SQL using C#.net,VS2005
- Q: Export DataTable to Excel using c# with Formatting Styles
- Q: Crystal report in c# .Net
- Q: Import excel into Sql server 2005 Table
- Q: READ, UPDATE & DELETE XML FILE USING X-PATH
- Q: Export Data from Listview to Excel
- Q: About GridView Data To Excel C#
- Q: Simple way to convert excel sheet to sql table
- Q: How to send data from c# file(.cs) to xslt file
- Q: How to Export and import data from sqlserver to excel sheet in vb.net
- Q: Migration of data from access to excel and vice versa
- Q: How to insert the data from excel to Database in .Net?
- Q: How can we create excel sheet in c# using data grid
- Q: DateTime Value Manipulation
- Q: create an array from a txt file
- Q: HowtoOepnandReadanExcelSpreadsheetinaListViewin.NET
- Q: How to fill data to excel
- Q: Reading Image from Excel file using C# .Net
- C# / CSharp Tutorial
- stackoverflow.com/questions
- General .NET Projects
- Sample .NET Projects with source code
- CSharpArticles
- PHPManual
- phpmanual/en/install.windows
- ProtoPage
Sunday, August 23, 2009
"PrintForm" Component, available in .NET 3.5 SP1
How to see Print Preview & Print a WindowsForm

using System.Drawing.Printing; //For "PrintDocument" Class
public partial class Form3 : Form
{
PrintDocument PrintDoc1 = new PrintDocument();
PrintPreviewDialog PPDlg1 = new PrintPreviewDialog();
public Form3()
{
InitializeComponent();
PPDlg1.Document = PrintDoc1;
PrintDoc1.OriginAtMargins =true; //To set or Get the Position of a Graphic Object
PrintDoc1.PrintPage += PDoc_PrintPage;
}
private void btn_PrintPreview_Click(object sender, EventArgs e)
{// When PrintPreview Button Clicks
PPDlg1.ShowDialog();
}
private void PDoc_PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bmp = new Bitmap(this.Width, this.Height);
//this.DrawToBitmap(bmp, this.ClientRectangle);
this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height)); //Takes the Snap of the Exact WindowForm size as Bitmap image
e.Graphics.DrawImage(bmp, 0, 0);
}
private void btn_PrintPage_Click(object sender, EventArgs e)
{//When Print Button Clicks, Image will be show & Ready to Print
PrintDoc1.Print();
}
}
Wednesday, August 19, 2009
Deployment of a Windows Forms / Console Application
On the Build menu, choose Rebuild all Projects, to make sure that the client application,
To create a deployment project
1. On the File menu, point to Add, and then click New Project.
2. In the Add New Project dialog box, in the Project Types pane, open the Other Project Types node, click Setup and Deployment Projects, then click Setup Project in the Templates pane. In the Name box, type <project_name>_Setup.
3. Click OK to close the dialog box.
The project is added to Solution Explorer, and the File System Editor opens.
4. Change ProductName property and remove "_Setup" from default ProductName name
· Select the <project_name>_Setup project in Solution Explorer. In the Properties window, select the ProductName property and type <project_name> without _Setup.
| Note: · The ProductName property specifies the name that will be displayed for the application in installation folder names and in the Add/Remove Programs dialog box. |
5. Change Manufacturer from ‘Default Company Name’ property to xxx . Then the MSI will install your application installed into the c:\Program Files\xxx\<project_name>\ folder
To add the WinForms / Console application to the installer
1. Right-click the \<project_name>_Setup project in Solution Explorer.
· View -> File System
· In the File System Editor, select the Application Folder node.
2. On the Action menu, point to Add, and then click Project Output.
3. In the Add Project Output Group dialog box:
- choose \<project_name> from the Project drop-down list
- Select the Primary Output group from the list
- In the Configuration box, select (Active).
- Click OK to close the dialog box.
To create Program Files folder & menu
Select the \<project_name>_Setup project in Solution Explorer.
1. Normally Install will, by default, restrict installation to a single user. If needed, in Project Explorer, click Setup Project name, view properties list, and set “InstallAllUsers” to true.
1a. In the File System Editor, right-click User’s Program Menu.
2. Add -> Folder and Rename Folder to <project_name>
3. In the File System Editor, in the Application Folder, select the Primary output from <project_name> node.
4. Right Click -> Create Shortcut to Primary Output.
5. Rename the Shortcut to <project_name>.
6. Select the Shortcut and drag it to your User’s Program Menu folder in the left pane.
To add an icon for Program Files folder & menu
- In the File System Editor, click User’s Program Menu. then in the right panel, right-click the shortcut (not the folder).
In the Properties window, select the Icon property and choose (Browse...) from the drop-down list. The Icon dialog box will be displayed.
In the DropDownListBox, choose "User's Program Menu"
Click the Add File Button, then browse for the Icon file.
The Icon dialog box will be displayed.
In the DropDownListBox, choose "User's Program Menu"
Click the Add File Button, then browse for the Icon file.
Rebuild the _setup project, then you should be ready !
..
Tuesday, August 11, 2009
Load Crystal Report from File
This code will show how to load a crystal report .rpt file that is saved on a local drive instead of embedded. The advantage to this is the program does not need to be re-compiled each time a report is modifed. Also, the .rpt can be upload from the application and stored in a database and then written to file. Do not embed the .rpt file when using this method.
using System;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace Report
{
public partial class Report : Document
{
public void ReportLoad()
{
ReportDocument reportDocument = new ReportDocument();
string filePath = "C:\Projects\Application\Report\CrystalReport.rpt";
reportDocument.Load(filePath);
crystalReportViewer.ReportSource = reportDocument;
}
}
}
..
Export Data from Listview to Excel
private void Export2Excel()
{
try
{
//lvPDF is nothing but the listview control name
string[] st = new string[lvPDF.Columns.Count];
DirectoryInfo di = new DirectoryInfo(@"c:\PDFExtraction\");
if (di.Exists == false)
di.Create();
StreamWriter sw = new StreamWriter(@"c:\PDFExtraction\" + txtBookName.Text.Trim() + ".xls", false);
sw.AutoFlush = true;
for (int col = 0; col < lvPDF.Columns.Count; col++)
{
sw.Write("\t" + lvPDF.Columns[col].Text.ToString());
}
int rowIndex = 1;
int row = 0;
string st1 = "";
for (row = 0; row < lvPDF.Items.Count; row++)
{
if (rowIndex <= lvPDF.Items.Count)
rowIndex++;
st1 = "\n";
for (int col = 0; col < lvPDF.Columns.Count; col++)
{
st1 = st1 + "\t" + "'" + lvPDF.Items[row].SubItems[col].Text.ToString();
}
sw.WriteLine(st1);
}
sw.Close();
FileInfo fil = new FileInfo(@"c:\PDFExtraction\" + txtBookName.Text.Trim() + ".xls");
if (fil.Exists == true)
MessageBox.Show("Process Completed", "Export to Excel", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
}
}
...
Load Data from Excel File to DataGridView
...
To read data from excel file and load that data into DataGridView, this function will take file name as parameter and reads that excel file
Controls Required: DataGridView
using System.Data.OleDb;
using System.IO;
public void setExcelFileAsDataSourceToDataGridView(string FileName)
{
OleDbConnection ocon;
OleDbDataAdapter oda;
DataSet ds;
//Check Whether file is xls file or not
if (Path.GetExtension(FileName) == ".xls")
{
try
{
//Create a OLEDB connection for Excel file
string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+"Data Source=" + FileName + ";" + "Extended Properties=Excel 8.0;";
oCon = new OleDbConnection(conStr);
oda = new OleDbDataAdapter("select * from [Sheet1$]",oCon);
ds = new DataSet();
//Fill the Data Set
oda.Fill(ds);
//Set DataSource of DataGridView
dataGridView1.DataSource = ds.Tables[0];
ds.Dispose();
oda.Dispose();
oCon.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("Please select Excel File");
}
}