Object disposed when trying to assign column width
-
Hi all, I have a datagridview on a form. I have a subroutine called LoadReloadProcs that has this line in it Me.dgvProcs.Columns(0).Width = Me.dgvProcs.Width * 0.75 This is the line where the error's happening. I'm getting an objectdisposed error. Funny thing is, it'll run through this routine twice without reporting an error. It only reports the error when I come back from running a report. Also, if I put a breakpoint right there and use the immediate window to check the value of the width (i.e., ? dgvProcs.Columns(0).Width) the system tells me that it is 100. But if I try to assign a value to the width (i.e., in the immediate window dgvProcs.Columns(0).Width = 435) the system throws the object disposed error. Why would I be able to request information of an object that is disposed? I shouldn't be able to, right? Or if it is not disposed, which being able to query the value of something suggests, why can't I assign a value to that property? This has me really confused. I've put the code for the procedure below... I don't see anything in there that helps me understand, but I figured one of y'all might. Also, I've searched for "dgvProcs.dispose" and just for ".dispose" and didn't find any relevant to dgvProcs.
Private Sub LoadReloadProcList(ByVal strsql As String)
Try
'Me.Controls.Remove(dgvProcs)
Dim daMyList = New SqlCeDataAdapter(strsql, myDB.cn)
Dim dsMyList = New DataSet
daMyList.Fill(dsMyList)
Dim myBinding As New BindingSource
myBinding.DataSource = dsMyList
Me.dgvProcs.DataSource = myBinding
Me.dgvProcs.DataMember = "Table"
Me.dgvProcs.Columns(0).HeaderText = "Name"
Me.dgvProcs.Columns(0).Width = Me.dgvProcs.Width * 0.75
Me.dgvProcs.Columns(1).HeaderText = "Barcode"
Me.dgvProcs.Columns(1).Width = Me.dgvProcs.Width * 0.15
Me.Controls.Add(dgvProcs)
Catch ex As Exception
#If DEBUG Then
MsgBox(ex.Message)
#Else
MsgBox("There was a problem gathering information on your previous procedures. If this problem persists please contact your distributor.")
#End If
End Try
End SubThanks in advance for any help you can give.
Denise "Hypermommy" Duggan
-
Hi all, I have a datagridview on a form. I have a subroutine called LoadReloadProcs that has this line in it Me.dgvProcs.Columns(0).Width = Me.dgvProcs.Width * 0.75 This is the line where the error's happening. I'm getting an objectdisposed error. Funny thing is, it'll run through this routine twice without reporting an error. It only reports the error when I come back from running a report. Also, if I put a breakpoint right there and use the immediate window to check the value of the width (i.e., ? dgvProcs.Columns(0).Width) the system tells me that it is 100. But if I try to assign a value to the width (i.e., in the immediate window dgvProcs.Columns(0).Width = 435) the system throws the object disposed error. Why would I be able to request information of an object that is disposed? I shouldn't be able to, right? Or if it is not disposed, which being able to query the value of something suggests, why can't I assign a value to that property? This has me really confused. I've put the code for the procedure below... I don't see anything in there that helps me understand, but I figured one of y'all might. Also, I've searched for "dgvProcs.dispose" and just for ".dispose" and didn't find any relevant to dgvProcs.
Private Sub LoadReloadProcList(ByVal strsql As String)
Try
'Me.Controls.Remove(dgvProcs)
Dim daMyList = New SqlCeDataAdapter(strsql, myDB.cn)
Dim dsMyList = New DataSet
daMyList.Fill(dsMyList)
Dim myBinding As New BindingSource
myBinding.DataSource = dsMyList
Me.dgvProcs.DataSource = myBinding
Me.dgvProcs.DataMember = "Table"
Me.dgvProcs.Columns(0).HeaderText = "Name"
Me.dgvProcs.Columns(0).Width = Me.dgvProcs.Width * 0.75
Me.dgvProcs.Columns(1).HeaderText = "Barcode"
Me.dgvProcs.Columns(1).Width = Me.dgvProcs.Width * 0.15
Me.Controls.Add(dgvProcs)
Catch ex As Exception
#If DEBUG Then
MsgBox(ex.Message)
#Else
MsgBox("There was a problem gathering information on your previous procedures. If this problem persists please contact your distributor.")
#End If
End Try
End SubThanks in advance for any help you can give.
Denise "Hypermommy" Duggan
You have a DataGridView on your form that YOU called
dgvProcs
. So, obviously searching the Internet for a name that you made up is going to be pointless. The error message says that you destroyed thedgvProcs
object and now you're trying to use it. The line that you commented out in your code snippet would do that if it wasn't commented out before.Me.Controls.Remove(dgvProcs)
would detroy thedgvProcs
object.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
You have a DataGridView on your form that YOU called
dgvProcs
. So, obviously searching the Internet for a name that you made up is going to be pointless. The error message says that you destroyed thedgvProcs
object and now you're trying to use it. The line that you commented out in your code snippet would do that if it wasn't commented out before.Me.Controls.Remove(dgvProcs)
would detroy thedgvProcs
object.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...I'm not so confused that I searched the internet for dgvProcs. I searched the code. Sorry I didn't make that clearer... I guess I just figured it was obvious. And yes, if that line weren't commented out I can see that it would do it. But that line's been commented out the whole time and I'm still getting the error. It's really wierd.
Denise "Hypermommy" Duggan
-
You have a DataGridView on your form that YOU called
dgvProcs
. So, obviously searching the Internet for a name that you made up is going to be pointless. The error message says that you destroyed thedgvProcs
object and now you're trying to use it. The line that you commented out in your code snippet would do that if it wasn't commented out before.Me.Controls.Remove(dgvProcs)
would detroy thedgvProcs
object.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Hi Dave,
Dave Kreskowiak wrote:
Me.Controls.Remove(dgvProcs) would detroy the dgvProcs object
I'm not with you here. Who would be doing the Dispose? Are you saying ControlCollection.Remove(x) actively calls x.Dispose()? (I can't see that in the doc) or x being removed becomes collectible? from the code
dgvProcs
is a class member, so this reference is still alive. Couldn't the problem be that by setting a new DataSource the Columns[] collection got lost? :confused:Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
I'm not so confused that I searched the internet for dgvProcs. I searched the code. Sorry I didn't make that clearer... I guess I just figured it was obvious. And yes, if that line weren't commented out I can see that it would do it. But that line's been commented out the whole time and I'm still getting the error. It's really wierd.
Denise "Hypermommy" Duggan
No, there could be no more references holding on to it. The other thing, that i didn't see earlier, is possibly the columns being wiped out and rebuilt after the data bind. And, of course, the possibility of having to Clean the project, then rebuild.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
No, there could be no more references holding on to it. The other thing, that i didn't see earlier, is possibly the columns being wiped out and rebuilt after the data bind. And, of course, the possibility of having to Clean the project, then rebuild.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Clean the project and rebuild? I'm afraid I don't understand. There's still a lot of tools in Visual Studio that I'm learning... is "clean" one of them? I do appreciate the assistance, believe me! I ended up finding a workaround... I noticed that if I clicked OK on the messagebox that informed me of the error, the system went on ahead and built everything and all looked and acted okay. So what I did was just put a separate catch block in there to catch just the object disposed exception and swallow it up and ignore it. That done, everything appears to work fine. Of course, the "solution" makes me a bit nervous as ignoring exceptions is probably not the safest thing to do..... But maybe it gives a clue to one of you gurus that says what's going on... 'cause this is still too weird to me.
Denise "Hypermommy" Duggan