In vb.net application, I have a dgv. the data from dgv are transferred to textboxes when row are selected. I print these textbox as report using e.graphics. after printing perticular row data , that row is removed from dgv. (i filtered the dgv by "NotPrinted").
I want with a single button command, all the rows data printed one by one untill the dgv becomes empty.
I tried much but not getting the result.

Hi Satyam,

if my understanding is correct maybe you can try this code:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim col1 As String, col2 As String, col3 As String
        'if dgv is autogenerated
        For i As Integer = 0 To DataGridView1.Rows.Count - 2
            'print row/s and remove one by one.
            If Not DataGridView1.CurrentRow.IsNewRow Then 'to avoid exception for new generated row
                'assuming there are 3 columns in a table
                col1 = DirectCast(DataGridView1.Rows(i).Cells("Column1").Value, String)
                col2 = DirectCast(DataGridView1.Rows(i).Cells("Column2").Value, String)
                col3 = DirectCast(DataGridView1.Rows(i).Cells("Column3").Value, String)
                print(col1, col2, col3)
                MessageBox.Show(col1 + ", " + col2 + ", " + col3)
                DataGridView1.Rows.RemoveAt(i)
                i -= 1
            End If
        Next

        'if not then modify the code:

        'For i As Integer = 0 To DataGridView1.Rows.Count - 1
        'If DataGridView1.Rows.Count > 0 Then
        'col1 = DirectCast(DataGridView1.Rows(i).Cells("Column1").Value, String)
        'col2 = DirectCast(DataGridView1.Rows(i).Cells("Column2").Value, String)
        'col3 = DirectCast(DataGridView1.Rows(i).Cells("Column3").Value, String)
        'print(col1, col2, col3)
        'MessageBox.Show(col1 + ", " + col2 + ", " + col3)
        'DataGridView1.Rows.RemoveAt(i)
        'i -= 1
        'End If
        'Next
    End Sub

    Private Sub print(col1 As String, col2 As String, col3 As String)
        'your code here...

    End Sub

Hardz

here i just forgot to say one thing...

my dgv is autogenerated e.g. its databindingsource.

I just put the same datasource into details view.(textboxes)

So both dgv and textbox display same data.

After print command the dgv row is removed also the data from textbox untill the dgv becomes empty

so the above code would also work?

Hi Satyam,

yes this code is working based on your requirements. But I have made a modification of the code to follow the requirements based on my understanding. So using Northwind database:

Imports System.Data.SqlClient

Public Class Form1

    'handle cellenter event for data selection from dgv to textboxes, just in case.
    Private Sub dataGridView1_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        If DataGridView1.Rows.Count - 1 > 0 Then
            TextBox1.Text = DataGridView1.CurrentRow.Cells("ProductName").Value.ToString()
            TextBox2.Text = DataGridView1.CurrentRow.Cells("CompanyName").Value.ToString()
            TextBox3.Text = DataGridView1.CurrentRow.Cells("CategoryName").Value.ToString()
            TextBox4.Text = DataGridView1.CurrentRow.Cells("Description").Value.ToString()
        End If
    End Sub

    Private conn As SqlConnection
    'sample connection string using northwind database
    Private strConn As String = "Data Source = IT-Hardz\SqlExpress; Database = 'NORTHWND'; Integrated Security = True"

    'loading/filtering data from the database
    Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click
        Using conn = New SqlConnection(strConn)
            conn.Open()
            Using cmd As New SqlCommand("Select ProductName, CompanyName, CategoryName, Description From " +
                                        "Products_By_Category_Supplier Where Description Like '%' + @desc + '%' ", conn)
                cmd.CommandTimeout = 3600
                cmd.Parameters.AddWithValue("@desc", txtFilter.Text)
                Dim da As New SqlDataAdapter()
                Dim ds As New DataSet()
                da.SelectCommand = cmd
                da.Fill(ds, "dsPro")
                DataGridView1.DataSource = ds.Tables("dsPro")
            End Using
        End Using
    End Sub

    'printing all data from dgv to textboxes.
    Private Sub btnPrintAll_Click(sender As System.Object, e As System.EventArgs) Handles btnPrintAll.Click
        'if dgv is autogenerated
        For i As Integer = 0 To DataGridView1.Rows.Count - 2
            'print row/s and remove one by one.
            If DataGridView1.Rows.Count - 1 > 0 Then
                'to avoid exception for new generated row
                TextBox1.Text = DataGridView1.Rows(i).Cells("ProductName").Value.ToString()
                TextBox2.Text = DataGridView1.Rows(i).Cells("CompanyName").Value.ToString()
                TextBox3.Text = DataGridView1.Rows(i).Cells("CategoryName").Value.ToString()
                TextBox4.Text = DataGridView1.Rows(i).Cells("Description").Value.ToString()
                print()
                DataGridView1.Rows.RemoveAt(i)
                i -= 1
            End If
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
        Next
    End Sub

    Private Sub print()
        'your code here for printing using e graphics...

        MessageBox.Show(textBox1.Text + textBox2.Text + textBox3.Text + textBox4.Text)
    End Sub
End Class

Hardz

Hi, i modified the code as per your code.

Here is my complete code for autogenerate DGV in Filter it and print.

But the printall code is not working..

I am getting nothing.

Imports System.Data.OleDb
Imports System.Data
Public Class PrintHPReport

    Private Sub VerifiedHPReportBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VerifiedHPReportBindingNavigatorSaveItem.Click

        Me.Validate()
        Me.VerifiedHPReportBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.PrintHPRep)

    End Sub

    Private Sub PrintHPReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'TODO: This line of code loads data into the 'PrintHPRep.VerifiedHPReport' table. You can move, or remove it, as needed.
        Me.VerifiedHPReportTableAdapter.Fill(Me.PrintHPRep.VerifiedHPReport)

       ' to filter data

        Me.VerifiedHPReportBindingSource.Filter = "[PrintStatus] = '" & Me.Label2.Text & "'"


    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

            PrintDocument1.Print()

            Me.PrintStatusTextBox.Text = Label1.Text

            Me.Validate()
            Me.VerifiedHPReportBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.PrintHPRep)

    End sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage


       Dim font1 As Font = New Drawing.Font("Arial", 11, FontStyle.Underline)
        Dim font2 As Font = New Drawing.Font("Microsoft Sans Serif", 10)
        Dim font3 As Font = New Drawing.Font("arial", 10, FontStyle.Bold)

         e.Graphics.DrawString(Me.PatientNameLabel.Text, font3, Brushes.Black, 50, 195)
        e.Graphics.DrawString(Me.PatientNameTextBox.Text, font1, Brushes.Black, 150, 195)

    'your code
    Private Sub btnPrintall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintall.Click


        For i As Integer = 0 To VerifiedHPReportDataGridView.Rows.Count - 2
            'print row/s and remove one by one.
            If VerifiedHPReportDataGridView.Rows.Count - 1 > 0 Then
                'to avoid exception for new generated row
                PatientNameTextBox.Text = VerifiedHPReportDataGridView.Rows(i).Cells("PatientName").Value.ToString()
                AgeTextBox.Text = VerifiedHPReportDataGridView.Rows(i).Cells("Age").Value.ToString()
                PrintDocument1.Print()
                VerifiedHPReportDataGridView.Rows.RemoveAt(i)
                i -= 1
            End If
            PatientNameTextBox.Text = ""
            AgeTextBox.Text = ""
        Next
    End Sub

would you please modify my above code??

Since you are using a typed dataset, you just need to modify your dgv Design name. Right click to your dgv and select edit columns, or from Properties menu, select Columns then press Collection ellipse button. At edit columns Property menu, click on the PatientName then change its Design (Name) column to PatientName for example, and follow the same procedure with the next column.

For i As Integer = 0 To VerifiedHPReportDataGridView.Rows.Count - 2
            'print row/s and remove one by one.
            If VerifiedHPReportDataGridView.Rows.Count - 1 > 0 Then
                'to avoid exception for new generated row
                PatientNameTextBox.Text = VerifiedHPReportDataGridView.Rows(i).Cells(PatientName.Name).Value.ToString()
                AgeTextBox.Text = VerifiedHPReportDataGridView.Rows(i).Cells(Age.Name).Value.ToString()
                PrintDocument1.Print()
                'MessageBox.Show("Success...")
                VerifiedHPReportDataGridView.Rows.RemoveAt(i)
                i -= 1
            End If
            PatientNameTextBox.Text = ""
            AgeTextBox.Text = ""
        Next

Hardz

Thanks your code worked..

I also want help in some of this matter pl..

In my form I have PrintDttextbox to save date of Print.(date selected from datetimepicker)

Code is-

PrintDtTextBox.Text = DateTimePicker1.Text

So the date of print shold be saved in PrintTextBox and Printstatus as "Printed".

Code-

Me.PrintStatusTextBox.Text = Label1.Text

Finally every report should display date and status of print .

I used this code to save/update the data in dgv

Me.Validate()
Me.VerifiedHPReportBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.PrintHPRep)

How can add my code in your code?

Hi,

try this:

Private Sub BtnPrintAll_Click(sender As System.Object, e As System.EventArgs) Handles BtnPrintAll.Click
        For i As Integer = 0 To VerifiedHPReportDataGridView.Rows.Count - 2
            'print row/s and remove one by one.
            If VerifiedHPReportDataGridView.Rows.Count - 1 > 0 Then
                'to avoid exception for new generated row
                PatientNameTextBox.Text = VerifiedHPReportDataGridView.Rows(i).Cells(PatientName.Name).Value.ToString()
                AgeTextBox.Text = VerifiedHPReportDataGridView.Rows(i).Cells(Age.Name).Value.ToString()
                PrintDtTextBox.Text = DateTimePicker1.Text
                Me.PrintStatusTextBox.Text = Label1.Text
                PrintDocument1.Print()
                'MessageBox.Show("Success...")
                'after print you can insert this code automatically.
                Me.Validate()
                Me.VerifiedHPReportBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.PrintHPRep)

                'VerifiedHPReportDataGridView.Rows.RemoveAt(i) remove this code it won't be needed anymore since you are using filtering with typed dataset and to avoid deletion of row(s).

                i -= 1
            End If
            PatientNameTextBox.Text = ""
            AgeTextBox.Text = ""
            PrintDtTextBox.Text = ""
            PrintStatusTextBox.Text = ""
        Next
    End Sub

Hope it helps.

Hardz

This code works perfactly...
Thanks

One more question ??
In my form, I have Page2Combobox to be printed on another form, so if this combobox contain any text then it should be print (PrintDocument2 event and PrintDocument1)
Otherise only PrintDocument1 event.

How can I put this code?

The code in my previuous code-btnPrint event was

 If Page2RichTextBox.Text = "" Then
            PrintDocument1.Print()

            Me.PrintStatusTextBox.Text = Label1.Text

            Me.Validate()
            Me.VerifiedHPReportBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.PrintHPRep)
        Else
            PrintDocument1.Print()
            PrintDocument2.Print()

            Me.PrintStatusTextBox.Text = Label1.Text
            Me.Validate()
            Me.VerifiedHPReportBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.PrintHPRep)
        End If
Private Sub BtnPrint_Click(sender As System.Object, e As System.EventArgs) Handles BtnPrint.Click
        If String.IsNullOrEmpty(Page2Combobox.Text) Then
            PrintDocument1.Print()
            'MessageBox.Show("Printed document1")
            PrintDtTextBox.Text = DateTimePicker1.Text
            Me.PrintStatusTextBox.Text = Label1.Text
        Else
            'if not empty then print document1 and print document2
            PrintDocument1.Print()
            'MessageBox.Show("Printed document1")
            PrintDocument2.Print()
            'MessageBox.Show("Printed document2")
            PrintDtTextBox.Text = DateTimePicker1.Text
            Me.PrintStatusTextBox.Text = Label1.Text
        End If

        Me.Validate()
        Me.VerifiedHPReportBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.PrintHPRep)
    End Sub

    Private Sub PrintDocument2_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
        Dim font1 As Font = New Drawing.Font("Arial", 11, FontStyle.Underline)
        Dim font2 As Font = New Drawing.Font("Microsoft Sans Serif", 10)
        Dim font3 As Font = New Drawing.Font("arial", 10, FontStyle.Bold)
        e.Graphics.DrawString(Page2Combobox.Text, font1, Brushes.Black, 150, 195)
    End Sub

Hello Sir Hardz, Sir Satyam 1;

A clarification to the code above;

Have you included PrintPreviewDialog/PrintDialog/PrintDocument? and if I click the button(Print), what is inside of the DGV will be printed? What I mean is:

'Click Button Print
'Show PrintPreviewDialog included added content in PrintDocument
'Then Automatic start printing?
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.