Disposing a user control within a DataGridViewComboBoxColumn by ThinqLinq

Disposing a user control within a DataGridViewComboBoxColumn

By default, User controls dispose of all of the controls as part of their cleanup process. However, when using a DataGridViweComboBoxColumn, the column is still bound to the data source. While disposing you can encounter multiple exceptions as the values in each bound ComboBox cell is disposed if the ComboBox's datasource is disposed prior to the grid's binding source. To avoid this issue, make sure to force your BindingSource that the data grid is bound to to Nothing/Null at the beginning of the control's Dispose method. Below is a code excerpt.

Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
Me.MyGridViewBindingSource.DataSource = Nothing
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub

Note the third line. If this line is missing, the dispose of the datasources and the DataGridViewComboBoxColumn may not unbind in the correct order and thus raise unexpected exceptions. Remember that the Dispose is now in the hidden .Designer file. Naturally, this advice applies to VS2005 winforms.

Posted on - Comment
Categories: VB -
comments powered by Disqus