I tried using the files from the original post in order to print the output of my datagrid, but I get:
System.InvalidCastException: Specified cast is not valid
This is the line specified in the DataGrid class
For nextLine = _CurrentPrintGridLine To Min((_CurrentPrintGridLine + RowsPerPage(_PrintFont, e.Graphics)), CType(_DataGrid.DataSource, System.Data.DataTable).DefaultView.Count)
This is the code I am using:
Code:Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click 'Button that prints the datagrid If GridPrinter Is Nothing Then GridPrinter = New DataGridPrinter(Me.dgInventory) End If Try With Me.printDia 'printDia is PrintPreviewDialogue added manually .Document = GridPrinter.PrintDocument If .ShowDialog = DialogResult.OK Then GridPrinter.Print() End If End With Catch ex As System.InvalidCastException MsgBox(Convert.ToString(ex)) End Try End SubCode:Private Sub frmInvReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Create connection to Database Dim connStr As String Dim dbObjConn As New OleDb.OleDbConnection connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=InventoryManagement.mdb;" dbObjConn.ConnectionString = connStr 'create SQL statement Dim selectString As String = "Select * from Products" 'create DataAdapter Dim sqlDA As New OleDb.OleDbDataAdapter(selectString, connStr) 'Fill dataset Try dbObjConn.Open() Dim ds As New Data.DataSet sqlDA.Fill(ds, "Products") dgInventory.CaptionText = "Inventory Report" dgInventory.DataSource = ds.DefaultViewManager Catch ex As OleDb.OleDbException MsgBox(Convert.ToString(ex)) Finally dbObjConn.Close() End Try End Sub
So the datagrid is filled in properly, but when I try printing I get the cast error. If you need the whole project or more information please let me know.
Thanks![]()




Reply With Quote