Hi vijayprathaph...,
Using the below article i have created the example.
You need to add another DataGridView to the form which will be used to print the selected rows.
I have added a CheckBox column to the DataGridView for selecting the rows.
For more details on adding CheckBox refer below article.
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
this.BindDataGridView();
}
private void BindDataGridView()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
this.dataGridView1.DataSource = dt;
//Add a CheckBox Column to the DataGridView at the first position.
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "";
checkBoxColumn.Width = 30;
checkBoxColumn.Name = "checkBoxColumn";
dataGridView1.Columns.Insert(0, checkBoxColumn);
}
Bitmap bitmap;
private void btnPrint_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("Country");
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells["checkBoxColumn"].Value))
{
dt.Rows.Add(row.Cells[1].Value, row.Cells[2].Value, row.Cells[3].Value);
}
}
dataGridViewPrint.DataSource = dt;
//Resize DataGridView to full height.
int height = dataGridViewPrint.Height;
dataGridViewPrint.Height = dataGridViewPrint.RowCount * dataGridViewPrint.RowTemplate.Height;
//Create a Bitmap and draw the DataGridView on it.
bitmap = new Bitmap(dataGridViewPrint.Width, dataGridViewPrint.Height);
dataGridViewPrint.DrawToBitmap(bitmap, new Rectangle(0, 0, dataGridViewPrint.Width, dataGridViewPrint.Height));
//Resize DataGridView back to original height.
dataGridViewPrint.Height = height;
//Show the Print Preview Dialog.
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
private void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Print the contents.
e.Graphics.DrawImage(bitmap, 0, 0);
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BindDataGridView()
End Sub
Private Sub BindDataGridView()
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)),
New DataColumn("Name", GetType(String)),
New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Me.dataGridView1.DataSource = dt
Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = ""
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
dataGridView1.Columns.Insert(0, checkBoxColumn)
End Sub
Private bitmap As Bitmap
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Id")
dt.Columns.Add("Name")
dt.Columns.Add("Country")
For Each row As DataGridViewRow In dataGridView1.Rows
If Convert.ToBoolean(row.Cells("checkBoxColumn").Value) Then
dt.Rows.Add(row.Cells(1).Value, row.Cells(2).Value, row.Cells(3).Value)
End If
Next
dataGridViewPrint.DataSource = dt
'Resize DataGridView to full height.
Dim height As Integer = dataGridView1.Height
dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height
'Create a Bitmap and draw the DataGridView on it.
bitmap = New Bitmap(Me.dataGridView1.Width, Me.dataGridView1.Height)
dataGridView1.DrawToBitmap(bitmap, New Rectangle(0, 0, Me.dataGridView1.Width, Me.dataGridView1.Height))
'Resize DataGridView back to original height.
dataGridView1.Height = height
'Show the Print Preview Dialog.
printPreviewDialog1.Document = printDocument1
printPreviewDialog1.PrintPreviewControl.Zoom = 1
printPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles printDocument1.PrintPage
'Print the contents.
e.Graphics.DrawImage(bitmap, 0, 0)
End Sub
Screenshots
The Form
Print dialog with selected rows