DataGridView sometimes won't delete
I have a form on which I swap out a datagridview and a panel. There is a button on the form for this. This is for reporting some data. It works except when you perform a certain sequence of events and then click the swap button while the dgv is showing to switch to the panel, the dgv won't delete. Why wouldn't a datagridview delete? This code definitely works much of the time. And in the case where the dgv does not go away, I checked and the code is definitely being executed.
Code:
ReportDataGridView.Dispose()
ReportDataGridView = Nothing
Any ideas or anything I could do to troubleshoot this?
Re: DataGridView sometimes won't delete
Quote:
Originally Posted by
projecttoday
I have a form on which I swap out a datagridview and a panel. There is a button on the form for this. This is for reporting some data. It works except when you perform a certain sequence of events and then click the swap button while the dgv is showing to switch to the panel, the dgv won't delete. Why wouldn't a datagridview delete? This code definitely works much of the time. And in the case where the dgv does not go away, I checked and the code is definitely being executed.
Code:
ReportDataGridView.Dispose()
ReportDataGridView = Nothing
Any ideas or anything I could do to troubleshoot this?
What is the sequence of events that make the dgv not delete. Also, you need to post all the relevant code, not just 2 lines. The more information you provide, the better.
Re: DataGridView sometimes won't delete
I have found a working solution, although I don’t know why it works and the other way doesn’t.
If you’re interested: Does NOT work:
Code:
Private Sub ReportViewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReportViewButton.Click
If ReportView = "Scroll" Then
ReportView = "Page"
If Not IsNothing(ReportDataGridView) Then
ReportDataGridView.Dispose() <---------------------------- this is the delete that doesn't work
ReportDataGridView = Nothing
End If
If Not IsNothing(SortLabel) Then
SortLabel.Dispose()
SortLabel = Nothing
End If
NumRecsPerPage = 13
PanelWidth = 972
DisplayButtons()
ReadData()
PagN = 1
DisplayPage(PagN, dt, PanelWidth) ----------------------- puts a panel on the form
ReportViewButton.Text = "Scroll View"
ElseIf ReportView = "Page" Then
ReportView = "Scroll"
If Not IsNothing(ReportPanel) Then
ReportPanel.Dispose()
ReportPanel = Nothing
End If
If Not IsNothing(FooterPanel) Then
FooterPanel.Dispose()
FooterPanel = Nothing
End If
Scroll_View() --------------------------------------------------- puts a datagridview on the form
ReportViewButton.Text = "Page View"
End If
End Sub
This WORKS:
Code:
Private Sub ReportViewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReportViewButton.Click
If Not IsNothing(ReportDataGridView) Then
ReportDataGridView.Dispose()
ReportDataGridView = Nothing
End If
If Not IsNothing(SortLabel) Then
SortLabel.Dispose()
SortLabel = Nothing
End If
If Not IsNothing(ReportPanel) Then
ReportPanel.Dispose()
ReportPanel = Nothing
End If
If Not IsNothing(FooterPanel) Then
FooterPanel.Dispose()
FooterPanel = Nothing
End If
If ReportView = "Scroll" Then
ReportView = "Page"
NumRecsPerPage = 13
PanelWidth = 972
DisplayButtons()
ReadData()
PagN = 1
DisplayPage(PagN, dt, PanelWidth)
ReportViewButton.Text = "Scroll View"
ElseIf ReportView = "Page" Then
ReportView = "Scroll"
Scroll_View()
ReportViewButton.Text = "Page View"
End If
End Sub
As you can see the deletions are done unconditionally (except to test if there’s something to delete) in the version that works.
So I’ve got it working at least. I still don’t know why the divided deletes don’t work.
This is the process. The first 4 steps work fine:
Open the form in scroll (panel) view.
Click on page (datagridview) view.
Perform a search to filter the data.
Click on scroll view.
Click on page view. ------------ it executes but the scroll view (the datagridview) is still showing on top of the page view (the panel).
Re: DataGridView sometimes won't delete
I'm not sure why you would destroy the controls at all. Why not simply Hide and Show them or, if they have the same Location and Size, simply call BringToFront on the one you want to display and the other will be hidden behind it. That latter might cause issues with tab stops mind you.
Re: DataGridView sometimes won't delete
Working with controls on top of other controls in the designer would be difficult and there would have to be logic to hide/unhide controls on the last page. It's working fine now.
Re: DataGridView sometimes won't delete
Quote:
Originally Posted by
projecttoday
Working with controls on top of other controls in the designer would be difficult
No, it wouldn't. There's nothing complex about it. You simply add the controls to the form and set the Location and Size for at least one of them in the Properties window. You now have two controls occupying the same space. You can work on the one you're looking at and if you want to work on the other, simply right-click the one in front and select Send To Back or you can use the Document Outline window. There's really nothing to it.
Quote:
Originally Posted by
projecttoday
there would have to be logic to hide/unhide controls on the last page.
Huh? If you have a DataGridView and a Panel then you simply call Hide on one and Show on the other, or toggle their Visible properties. It's less complex than you have now.
Quote:
Originally Posted by
projecttoday
It's working fine now.
If that was the case then you wouldn't be here.