combobox gives unexpected cast error


G

Guest

using VS2005 - VB.Net

I noticed unexpected behaviour when using a combobox which has it's
datasource set to a datatable. Also the Valuemember and Displaymember are
being set. I'm getting a cast error when trying to read the SelectedValue.
However, if I change the order in which DataSource, Valuemember and
Displaymemeber are set, then the cast error is not thrown. What is happening
here?

Example (the error appears at ***) where ID is an integer, Name is a string:

Public Class FrmTestComboBox

Private Sub FrmTestComboBox_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
FillCombo()
End Sub

Public Sub FillCombo()
ComboBox1.DataSource = MyTable
ComboBox1.ValueMember = "ID"
ComboBox1.DisplayMember = "Name"
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
Dim val As Integer
Dim row As DataRowView

If ComboBox1.SelectedIndex > -1 Then
*** val = CInt(ComboBox1.SelectedValue)
row = DirectCast(ComboBox1.SelectedItem, DataRowView)
Label1.Text = CStr(row("Name"))
End If
End Sub
End Class


If I rewrite this (setting DataSource as the last line), it works as expected.

Public Sub FillCombo()
ComboBox1.ValueMember = "ID"
ComboBox1.DisplayMember = "Name"
ComboBox1.DataSource = MyTable
End Sub

I did not expect the order of the lines of code would have any consequence
here ...
Also in the Documentation an example shows up (for a listbox) which first
sets the datasource, and then the valuemember and displaymember.
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/P_System_Windows_Forms_ListControl_ValueMember.htm

Martin
 
Ad

Advertisements

J

Jared Parsons [MSFT]

Hello Martin,
If I rewrite this (setting DataSource as the last line), it works as
expected.

Public Sub FillCombo()
ComboBox1.ValueMember = "ID"
ComboBox1.DisplayMember = "Name"
ComboBox1.DataSource = MyTable
End Sub
I did not expect the order of the lines of code would have any
consequence

When you set the DataSource property it's causincg the SelectedIndex event
to be raised. So your event handling code executes before you've told the
ComboBox which members in the table to bind to. When you re-order the statements
the properties are correctly set when the event is raised.
 
W

Walter Wang [MSFT]

Hi Martin,

Thank you for your post.

I think you're referring to following MSDN Library document:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.listcontrol.va
luemember(d=ide).aspx

I have verified that the sample code does have this issue, it will trigger
the SelectedValueChanged event three times if setting the DataSource first.

I have reported this issue to our MSDN team on this. Thank you for your
feedback! We strive to capture any and all product issues / product
feedback so as to ensure that we are continuously developing Microsoft
products to meet customer needs. This is exactly why feedback such as yours
is always taken very seriously.

Please feel free to post here if you need more help on this.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Ha Walter and Jared,

Ah ... thanks. Indeed, I did not realize the event fired at that specific
piece of code.
Learning something everyday ;-)
 
Ad

Advertisements

W

Walter Wang [MSFT]

Hi Martin,

Thank you for your update.

Please feel free to post here if you need more help or anything is unclear.

Have a nice day!


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top