Accessing a Datagrid from Within a Class

J

Jasper Jones

I'm really stuck on this, I just can't seem to figure it out. In the
code below I read the item property of a Datagrid I have on my form.
However, I get an error about the datagrid1 part saying "Reference to a
non-shared member requires an object reference." How do I go about
creating a reference to my datagrid? I'd be eternally grateful to
anyone who can help me with this as I just can't seem to make any
progress with this.

Here's my code:

Public Class PrintStatement
Inherits PrintDocument
Protected Overrides Sub OnPrintPage(ByVal e As
PrintPageEventArgs)
MyBase.OnPrintPage(e)

Dim fnt As New Font("Courier New", 8, FontStyle.Regular,
GraphicsUnit.Point)

Dim LineX As Integer = 0


'loop through every row in the dataset
Dim iCtr As Integer
For iCtr = 0 To dsCashSale.Tables("dtCashSale").Rows.Count
- 1
e.Graphics.DrawString(Datagrid1.Item(iCtr, 1), fnt,
Brushes.Black, 20, LineX)
LineX = LineX + 12
Next

fnt.Dispose()
' Indicate that there are no more pages.
e.HasMorePages = False

End Sub
End Class
 
K

Ken Tucker [MVP]

Hi,

Try something like this.

Public Class PrintStatement
Inherits PrintDocument

dim dg as datagrid
dim dscashsale as datagrid

public sub New(byval MyDatagrid as datagrid, MyDataSet as dataset)
dg=mydatagrid
dscashsale=mydataset
end sub

Protected Overrides Sub OnPrintPage(ByVal e As
PrintPageEventArgs)
MyBase.OnPrintPage(e)

Dim fnt As New Font("Courier New", 8, FontStyle.Regular,
GraphicsUnit.Point)

Dim LineX As Integer = 0


'loop through every row in the dataset
Dim iCtr As Integer
For iCtr = 0 To dsCashSale.Tables("dtCashSale").Rows.Count
- 1
e.Graphics.DrawString(dg.Item(iCtr, 1), fnt,
Brushes.Black, 20, LineX)
LineX = LineX + 12
Next

fnt.Dispose()
' Indicate that there are no more pages.
e.HasMorePages = False

End Sub
End Class

Ken
----------------------------
I'm really stuck on this, I just can't seem to figure it out. In the
code below I read the item property of a Datagrid I have on my form.
However, I get an error about the datagrid1 part saying "Reference to a
non-shared member requires an object reference." How do I go about
creating a reference to my datagrid? I'd be eternally grateful to
anyone who can help me with this as I just can't seem to make any
progress with this.

Here's my code:

Public Class PrintStatement
Inherits PrintDocument
Protected Overrides Sub OnPrintPage(ByVal e As
PrintPageEventArgs)
MyBase.OnPrintPage(e)

Dim fnt As New Font("Courier New", 8, FontStyle.Regular,
GraphicsUnit.Point)

Dim LineX As Integer = 0


'loop through every row in the dataset
Dim iCtr As Integer
For iCtr = 0 To dsCashSale.Tables("dtCashSale").Rows.Count
- 1
e.Graphics.DrawString(Datagrid1.Item(iCtr, 1), fnt,
Brushes.Black, 20, LineX)
LineX = LineX + 12
Next

fnt.Dispose()
' Indicate that there are no more pages.
e.HasMorePages = False

End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top