StrataFrame Forum

Why is this weirdness needed?

http://forum.strataframe.net/Topic6129.aspx

By Greg McGuffey - 1/23/2007

I pulled this code form the code that loads a combo/list from an enum (I think). There is mention of some .NET weirdness that requires this. I'm wondering if you could shed a bit of light on why this is done? (Setting the display member and value member to ZLS, then to their actual values, then setting datasource, then setting them again!)





Private Sub SetDataSource(ByVal combo As SF.ComboBox _

, ByVal DataSource As Object _

, ByVal DisplayMember As String _

, ByVal ValueMember As String)

combo.BeginUpdate()

combo.DataSource = Nothing

combo.DisplayMember = String.Empty

combo.ValueMember = String.Empty

combo.DisplayMember = DisplayMember

combo.ValueMember = ValueMember

combo.DataSource = DataSource

combo.DisplayMember = DisplayMember

combo.ValueMember = ValueMember

combo.EndUpdate()

End Sub

By StrataFrame Team - 1/24/2007

The problem generally occurs when there is an event handler on the combo's SelectedIndexChanged or SelectedValueChanged or other event that fires when the data source on the combo changes (or even if the combo is data-bound and the formatting can throw an exception).  If an exception is thrown from within the handler, .NET snuffs the exception (it doesn't ever bubble up) and the ComboBoxItemCollection is never properly populated with the display and value member values.  The value member is left as the raw collection object (the DataRowView), not the value of the property on the object.  The display member is then set to the ToString() of the raw object, so the combo box is then populated with several "{System.Data.DataRowView}" entries.  We added the code to attempt to circumvent the formatting errors that can occur when you set the data source.
By Greg McGuffey - 1/24/2007

Goodness, that must have been a fun one to track down! Thanks for the info!