Solved

Public DataGridView is Disposed By a Class and Not Available Later

Posted on 2014-02-21
6
242 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

786 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