Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Master Details Forms with 2 DataGridView Child Grid doesn't clear when Clear the parent Grid
 

Master Details Forms with 2 DataGridView Child Grid doesn't clear when Clear the parent Grid

Hi everyone,

Well this is my problem:

I have a form with 3 BindingSources whose datasources are Custom Objects:

Component
-> Details (BindingList)
-> SubDetails (BindingList, too)

First BindingSource is bounded to the Form
Second one is bounded to the Details Grid
Third one is bounded to the >SubDetails Grid, like this:

Me.BindingSource.DataSource = Me._Component

Me.BindingSourceDetails.DataSource = Me.BindingSource
Me.BindingSourceDetails.DataMember = "Details"

Me.BindingSourceSubDetails.DataSource = Me.BindingSourceDetails
Me.BindingSourceSubDetails.DataMember = "SubDetails"

And I have 2 DataGridViews:

Me.DataGridViewDetails.DataSource = Me.BindingSourceDetails

Me.DataGridViewSubDetails.DataSource = Me.BindingSourceSubDetails

All woks fine except when the Component is cleared (all properties to default or empty values and Details collection is cleared), the DataGridViewDetails is cleared, but the DataGridViewSubDetails keeps showing the last rows.

I've looked the BindingSourceDetails content (after the clear operation) and apparently is all good: List count = 0, Position = -1, Current = Nothing, however the BindingSourceSubDetails shows bad information (the same that is showed by the DataGridViewSubDetails) even though the DataSource references point to BindingSourceDetails correctly.

Why does happen this behavior?

Anybody can help me please with any workaround or solution?

Thanks.


Jfk.Net  Wednesday, September 30, 2009 3:45 PM
Hello Jfk.Net,

Thank you for posting me the detail information. I have made a sample solution and upload it to the skydrive, you can download the whole sample here.
http://cid-380a93ce0876cf8b.skydrive.live.com/self.aspx/Microsoft%20MSDN%20Work/Solution%20Code/DGVTest%5E52009.10.2%5E6.zip

When I test the code, I added a method called Clear into Parent class.
Public Sub Clear()
        Me.Details.Clear()
End Sub

I just call Parent.Details.Clear method when I want to clear all rows. The rows in both DataGridView disappear.

You can compare it with your own code. If you have any doubt, please feel free to tell me.

Sincerely,
Kira Qian
Send us any feedback you have about the help from MSFT at EMAIL REMOVED
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
  • Marked As Answer byJfk.Net Tuesday, October 06, 2009 1:38 PM
  •  
Kira Qian  Friday, October 02, 2009 2:45 AM
Hi Jfk.Net,

Could you please show me the code of your custom data source?

When I tested the scenario on my side, I used a BindingList(Of MyObj) as my data source and it works fine. The data in the DataGridViewDetails and DataGridViewSubDetails had been cleared when I press a button to clear the BindingList. So if I can get the code of your custom data source, I can go on further test to find the problem.

I am waiting for your reply.

Sincerely,
Kira Qian
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Thursday, October 01, 2009 6:48 AM
Hi Kira Qian

Thanks for you support,

Here is an abstract example of my code:

Public Class Parent

Private _Id As Integer
Public Property Id As Integer
Get
Return Me._Id
End Get
Set (value As Integer)
Me._Id = value
End Set
End Property

Private _Name As String = String.Empty
Public Property Name As String
Get
Return Me._Name
End Get
Set (value As String)
Me._Name = value
End Set
End Property

Private _Details As BindingList(Of Child)
Public ReadOnly Property Details As BindingList(Of Child)
Get
If _Details Is Nothing Then
_Details = New BindingList(Of Child)
End If
Return _Details
End Get
End Property

Public Sub Initialize()
Me.Id = 0
Me.Name = String.Empty
Me.Details.Clear()
End Sub

End Class

Public Class Child

Private _Id As Integer
Public Property Id As Integer
Get
Return Me._Id
End Get
Set (value As Integer)
Me._Id = value
End Set
End Property

Private _Name As String = String.Empty
Public Property Name As String
Get
Return Me._Name
End Get
Set (value As String)
Me._Name = value
End Set
End Property

Private _SubDetails As BindingList(Of GrandChild)
Public ReadOnly Property SubDetails As BindingList(Of GrandChild)
Get
If _SubDetails Is Nothing Then
_SubDetails = New BindingList(Of GrandChild)
End If
Return _SubDetails
End Get
End Property

Public Sub Initialize()
Me.Id = 0
Me.Name = String.Empty
Me.SubDetails.Clear()
End Sub

End Class

Note: My real clases implement the INotifyPropertyChanged for changes in Id and Name properties, but this is the main idea.

In my form I have the code that I've indicated in the previuos post and I have a method in which I invoke the Initialize() Parent method.

Public Class Form1

Private _Component As Parent

Protected Overrides OnLoad(e as EventArgs)
Me._Component = New Parent
End Sub

Private Sub InitializeBusinessComponent()
Me._Component.Initialize()
End Sub

.
.
.

End Class

I hope these details can help. Can you share the code that you tested for compare my clases?

Thanks again.


Jfk.Net  Thursday, October 01, 2009 2:20 PM
Hello Jfk.Net,

Thank you for posting me the detail information. I have made a sample solution and upload it to the skydrive, you can download the whole sample here.
http://cid-380a93ce0876cf8b.skydrive.live.com/self.aspx/Microsoft%20MSDN%20Work/Solution%20Code/DGVTest%5E52009.10.2%5E6.zip

When I test the code, I added a method called Clear into Parent class.
Public Sub Clear()
        Me.Details.Clear()
End Sub

I just call Parent.Details.Clear method when I want to clear all rows. The rows in both DataGridView disappear.

You can compare it with your own code. If you have any doubt, please feel free to tell me.

Sincerely,
Kira Qian
Send us any feedback you have about the help from MSFT at EMAIL REMOVED
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
  • Marked As Answer byJfk.Net Tuesday, October 06, 2009 1:38 PM
  •  
Kira Qian  Friday, October 02, 2009 2:45 AM
Hi Kira Qian

Your example help me a lot I had a issue with ListChanged and PropertyChanged events - I invoked to PropertyChanged into a ListChanged event of my Details Collection -, but I've just corrected that.

Issue:

Private Sub _Details_ListChanged(sender As Object, e As ListChangedEventArgs)
 -- This nullify synchronization of the BindingSources when it removes the only one item of Details Collection or when this is cleared:
 Me.OnPropertyChanged("Details")
End Sub

Corrected:

Private Sub _Details_AnotherEventAfterListChanged(sender As Object, e As ListChangedEventArgs)
 Me.OnPropertyChanged("Details")
End Sub


Thanks so much.

Jfk.Net
Jfk.Net  Tuesday, October 06, 2009 1:46 PM

You can use google to search for other answers

Custom Search

More Threads

• DataBind with Customization in Datagridview
• data reader with richtextbox
• Databindings of combobox and enum in c#
• Problem with updates
• datagridview of listview?
• How do I enable typing in the combo box cell?
• DataGridView: remove the padding in RowHeaderCell?
• Concurrency algorithm
• Pen object scaling issue
• GridView Conditional Formatting