|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
combobox gives unexpected cast errorI 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 -- It''s Software Jim, but not as we know it! Hello Martin,
> If I rewrite this (setting DataSource as the last line), it works as When you set the DataSource property it's causincg the SelectedIndex event > 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 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. -- Jared Parsons [MSFT] jared***@online.microsoft.com All opinions are my own. All content is provided "AS IS" with no warranties, and confers no rights. 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 (waw***@online.microsoft.com, 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. Ha Walter and Jared,
Ah ... thanks. Indeed, I did not realize the event fired at that specific piece of code. Learning something everyday ;-) -- It''s Software Jim, but not as we know it! 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 (waw***@online.microsoft.com, 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.
Redrawing graphics in userControl - Button
How to use ADO command object to get a record set via stored procedure? start new thread help A question about sockets, listener VS 2005 Property question datasets web browser control auto scroll How to convert ShortPath to longPath? ListView iteration order |
|||||||||||||||||||||||