?
Solved

Public DataGridView is Disposed By a Class and Not Available Later

Posted on 2014-02-21
6
Medium Priority
?
249 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
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 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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