i have static flow layout panel and i select data from datagridview and send to print.
after click on print button i create dynamic panel inside the flowlayout accordind to select data.
and i want if i select 10 data fromm gridview then 5 panel print to first page and then next data print to next page .
code sample here.
private void btn_print_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dgv_preview.Rows)
{
bool isSelected = Convert.ToBoolean(row.Cells["checkBoxColumn"].Value);
if (isSelected)
{
flowLayoutPanel2.Visible = true;
Panel flp = new Panel();
flp.Name = "flp" + counter;
flp.Width = 800;
flp.Height = 212;
flp.Anchor = AnchorStyles.None;
flp.BackgroundImage = Properties.Resources.icard2;
flp.BackgroundImageLayout = ImageLayout.Zoom;
Label lbl = new Label();
lbl.Name = "lbl" + counter;
lbl.Font = new Font("Kruti Dev 010", 10);
lbl.Text = "???? ?????";//row.Cells["name"].Value.ToString();
lbl.FlatStyle = FlatStyle.Flat;
lbl.ForeColor = Color.Black;
lbl.BackColor = Color.Transparent;
lbl.Location = new Point(240, 70);
lbl.AutoSize = true;
lbl.Size = new Size(lbl.PreferredWidth, lbl.PreferredHeight);
Label lbl1 = new Label();
lbl1.Name = "lbl1" + counter;
lbl1.Text = row.Cells["Full Name"].Value.ToString();
lbl1.ForeColor = Color.Black;
lbl1.FlatStyle = FlatStyle.Flat;
lbl1.AutoSize = true;
lbl1.BackColor = Color.Transparent;
lbl1.Location = new Point(240, 81);
lbl1.Size = new Size(lbl1.PreferredWidth, lbl1.PreferredHeight);
lbl1.Font = new Font("Arial", 9, FontStyle.Bold);
Label lbl2 = new Label();
lbl2.Name = "lbl2" + counter;
lbl2.Font = new Font("Kruti Dev 010", 10);
lbl2.Text = row.Cells["rankh"].Value.ToString();
lbl2.ForeColor = Color.Black;
lbl2.FlatStyle = FlatStyle.Flat;
lbl2.AutoSize = true;
lbl2.BackColor = Color.Transparent;
lbl2.Location = new Point(240, 98);
lbl2.Size = new Size(lbl2.PreferredWidth, lbl2.PreferredHeight);
Label lbl3 = new Label();
lbl3.Name = "lbl3" + counter;
lbl3.Text = row.Cells["Rank"].Value.ToString();
lbl3.ForeColor = Color.Black;
lbl3.FlatStyle = FlatStyle.Flat;
lbl3.AutoSize = true;
lbl3.BackColor = Color.Transparent;
lbl3.Location = new Point(240, 109);
lbl3.Size = new Size(lbl3.PreferredWidth, lbl3.PreferredHeight);
lbl3.Font = new Font("Arial", 9, FontStyle.Bold);
PictureBox pc4 = new PictureBox();
var cardno = row.Cells["Force No"].Value.ToString();
var a = cardno.Count();
if (a == 9)
{
pc4.Image = Properties.Resources.force;
}
else
{
pc4.Image = Properties.Resources.irla;
}
pc4.Name = "pc4" + counter;
pc4.Width = 33;
pc4.Height = 15;
pc4.BackColor = Color.White;
pc4.BackColor = Color.Transparent;
//pc4.Image = Properties.Resources.irla;
pc4.SizeMode = PictureBoxSizeMode.StretchImage;
pc4.Location = new Point(188, 138);
flowLayoutPanel2.Controls.Add(flp);
flp.Controls.Add(lbl);
flp.Controls.Add(lbl1);
flp.Controls.Add(lbl2);
flp.Controls.Add(lbl3);
flp.Controls.Add(pc4);
counter++;
// }
}
}
PrintAllImages();
}
private int imagesToPrintCount;
private void PrintAllImages()
{
imagesToPrintCount = flowLayoutPanel2.Controls.Count;
flowLayoutPanel2.Height = 220 * 5;
System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
doc.PrintPage += Document_PrintPage;
PrintDialog dialog = new PrintDialog();
dialog.Document = doc;
if (dialog.ShowDialog() == DialogResult.OK)
doc.Print();
}
private void Document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
RectangleF bounds = e.PageSettings.PrintableArea;
//float factor = ((float)bmp.Height / (float)bmp.Width);
//e.HasMorePages = true;
//e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
e.Graphics.DrawImage(GetNextImage(), e.MarginBounds);
e.HasMorePages = imagesToPrintCount > 0;
flowLayoutPanel2.Visible = false;
dgv_preview.Visible = true;
}
private Bitmap GetNextImage()
{
Bitmap bmp = new Bitmap(flowLayoutPanel2.Width, flowLayoutPanel2.Height, flowLayoutPanel2.CreateGraphics());
flowLayoutPanel2.DrawToBitmap(bmp, new Rectangle(0, 0, flowLayoutPanel2.Width, flowLayoutPanel2.Height));
//PictureBox pictureBox = (PictureBox)flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - imagesToPrintCount];
imagesToPrintCount = imagesToPrintCount - 5;
return bmp;
}
HOW TO I PRINT ALL SELECTED DATA.
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Archit ShrivastavaPosted Aug 24, 2018, 5:04 AM
Jitendra KumarPosted Aug 24, 2018, 4:39 AM
It could be little complex, and I wrote small sample here.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication22
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
flowLayoutPanel1.Scroll += new ScrollEventHandler(flowLayoutPanel1_Scroll);
}
int height;
Bitmap bitmap;
int y = 0;
private void Form2_Load(object sender, EventArgs e)
{
height = tableLayoutPanel1.Height;
bitmap = new Bitmap(tableLayoutPanel1.Width, tableLayoutPanel1.Height);
}
void flowLayoutPanel1_Scroll(object sender, ScrollEventArgs e)
{
height -= flowLayoutPanel1.Height;
if (y + flowLayoutPanel1.Height < tableLayoutPanel1.Height - flowLayoutPanel1.Height)
{
y += flowLayoutPanel1.Height;
}
else
{
y = tableLayoutPanel1.Height - flowLayoutPanel1.Height;
}
}
private void button1_Click(object sender, EventArgs e)
{
flowLayoutPanel1.DrawToBitmap(bitmap, new Rectangle(0, y, flowLayoutPanel1.Width, flowLayoutPanel1.Height));
while (height > flowLayoutPanel1.Height)
{
SendMessage(this.flowLayoutPanel1.Handle, WM_VSCROLL, SB_PAGEDOWN, 0);
flowLayoutPanel1.DrawToBitmap(bitmap, new Rectangle(0, y, flowLayoutPanel1.Width, flowLayoutPanel1.Height));
}
SendMessage(this.flowLayoutPanel1.Handle, WM_VSCROLL, SB_TOP, 0);
height = tableLayoutPanel1.Height;
y = 0;
PictureBox pic = new PictureBox();
pic.Width = tableLayoutPanel1.Width;
pic.Height = tableLayoutPanel1.Height;
pic.Image = bitmap;
Form newform = new Form();
newform.Text = "Image";
newform.Controls.Add(pic);
newform.Show();
}
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
IntPtr hWnd, // handle to destination window
uint Msg, // message
uint wParam, // first message parameter
uint lParam // second message parameter
);
private const int WM_VSCROLL = 0x0115;
private const int SB_TOP = 6;
private const int SB_PAGEDOWN = 3;
}
}