Solved

Public DataGridView is Disposed By a Class and Not Available Later

Posted on 2014-02-21
6
244 Views
Last Modified: 2014-03-02
I am copying a DataGridView on a visible UserControl to a Public DataGridView within a Class, which eventually is disposed.  The public datagrid is defined as:

Public ucgrid as DataGridView

Open in new window


When the user control is visible, within the instantiated class, I fetch the datagridview from the usercontrol's datagrid using the following code (there's only one datagridview):

 For Each cntl As Control In CurrentUCVisible.Controls
    If TypeOf (cntl) Is DataGridView Then
       ucgrid = cntl
       For i = 2 To NumFeatures
          For j = 1 To i - 1
              MsgBox(ucgrid.Rows(i - 1).Cells(j - 1).Value)  'Valid data entries here
          Next
       Next
   End If
Next

Open in new window


Later, when I spawn the user control again and attempt to load the Public datagridview "ucgrid" into the usercontrol's property (called "dgview") there are no rows and columns in it:

Dim myuc As New UserControlLibrary.myusercontrol
If reload = True Then
    myuc.dgview = ucgrid
    For i = 2 To NumFeatures
       For j = 1 To i - 1
            MsgBox(ucgrid.Rows(i - 1).Cells(j - 1).Value)  'blank entries here
       Next
   Next
   
End If

Open in new window


Question: since I am copying a datagridview form a user control into into a public datagridview, why are the elements missing?  I think that when the class is disposed (which copies the usercontrol's datagridview to the public datagridview) the public datagridview is getting disposed or wiped out somehow.  

Either way, there are valid data in the public datagridview within the instantiated class, but later on when the class is dispose, the public datagridview is empty.  

So why is a public object losing its content when that class used to load it is disposed?
0
Comment
Question by:lep1
  • 4
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Dennis Aries
ID: 39879748
Upon disposing, all elements associated with that object are usually disposed as well. Before disposing, set the reference in the object that you are about to dispose to nothing. That should solve your problem.
0
 

Author Comment

by:lep1
ID: 39879773
Dennis,

I looked further into the the code, and t works out that the copying of the datagridview1 (which shows valid data using msgbox) occurs in a subroutine of Form1 and not a class, so why does a Public datagridview have no data in the cells?    For some reason, the Public datagridview is being killed(?).

What code do you recommend for copying a datagridview from a visible UC form, saving in memory via Public (via a separate module), and then setting to a property of a newly launched UC form?

Thx, L
0
 
LVL 18

Expert Comment

by:Dennis Aries
ID: 39879781
I'm not certain about your snippets. If ucGrid should be filled in the last one, then it could also be used in the middle snippet. However, you use that snippet to set the value if ucGrid.

This would imply that you need to call both routines in order to get to your data. So, rather than being killed, it looks to me that the ucGrid has never been set at all at your subsequent spawns.

Dennis
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lep1
ID: 39879791
Dennis,

In line 3 of the 2nd snippet, the "cntl" is copied to the Public DataGridView called "ucgrid."   It simply works out that the ucgrid has nothing in it(?)

Maybe I'll have to write an example code which copies cell data from a DataViewGrid to a Public DataViewGrid(?)

Thx, L
0
 
LVL 18

Accepted Solution

by:
Dennis Aries earned 500 total points
ID: 39879800
I noticed that, but when is the second snippet being called?

You are probably better off creating a property that directly exposes the datagridview instead of copying it.

[snippet]
Public Property ucGrid() As DataGridView
  Get
   For Each cntl As Control In CurrentUCVisible.Controls
    If TypeOf (cntl) Is DataGridView Then
       ucgrid = cntl
       For i = 2 To NumFeatures
          For j = 1 To i - 1
              MsgBox(ucgrid.Rows(i - 1).Cells(j - 1).Value)  'Valid data entries here
          Next
       Next
   End If
Next
  End Get
End Property
[/snippet]
0
 
LVL 18

Expert Comment

by:Dennis Aries
ID: 39879802
Omit the MsgBox of course ;)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question