hi everybody,
I am new at c# and I will be tankfull if u help me to solve this problem.
I have a windows form which has a textbox ,datagrid and datepicker calender.
the user enter information in datagrid,textbox and ofcourse select a date.
the datagrid and all the form is right to left.
I want to print all context of textbox ,datetime picker and datagrid,and I DONT KNOW HOW TO DO THIS
can u help me?
tnx.
Loading
Mamta MPosted Nov 4, 2008, 6:16 AM
Hi,
What you need to do is: Add a PrintDocument control and a PrintDialog control. Then in the PrintPage event, write the following code. Call PrintDocument1.Print() when you want to actually begin printing.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e){
int x=10, y=10; // coordinates where to print Font font= new Font("Times New Roman", 12.0F); //set the font Graphics g = e.Graphics; SolidBrush brush = new SolidBrush(Color.BlueViolet); // print the date time and textg.DrawString(dateTimePicker1.Text, font, brush,
new Point(x, y));x += 20;
y += 30;
g.DrawString(textBox1.Text,font, brush,
new Point(x, y)); // retrieve data from the data grid cell by cell BindingSource bs = (BindingSource) dataGridView1.DataSource; DataSet ds = (DataSet)bs.DataSource; DataTable dt = ds.Tables[0];x += 30;
y += 30;
int originalx;originalx = x;
foreach (DataRow dr in dt.Rows){
x = originalx;
// so that each row starts at the same left coordinates foreach (DataColumn dc in dt.Columns){
g.DrawString(dr[dc].ToString(), font, brush,
new Point(x, y)); SqlCommand cmd = new SqlCommand("select a.precision from sys.all_columns a INNER JOIN sys.tables b ON a.object_id=b.object_id and b.name='Products' and a.name='"+ dc.ColumnName+"'"); SqlConnection conn = new SqlConnection();conn.ConnectionString = Properties.
Settings.Default.AdventureWorksConnectionString;cmd.Connection = conn;
conn.Open();
int size = Convert.ToInt32(cmd.ExecuteScalar()); if (size != 0){
x += dr[dc].ToString().Length +
Convert.ToInt32(size * font.Size);}
elsex += dr[dc].ToString().Length + dc.MaxLength+10;
}
y += 25;
// for the next column}
}
Hope this helps.
-Mamta