Solved

Public DataGridView is Disposed By a Class and Not Available Later

Posted on 2014-02-21
6
239 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now