Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Public DataGridView is Disposed By a Class and Not Available Later

Posted on 2014-02-21
6
Medium Priority
?
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 1500 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

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!

Question has a verified solution.

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

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…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

597 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