473,321 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

combobox gives unexpected cast error

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

--
It''s Software Jim, but not as we know it!
Jul 10 '06 #1
4 1321
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.

--
Jared Parsons [MSFT]
ja******@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Jul 10 '06 #2
Hi Martin,

Thank you for your post.

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

http://msdn2.microsoft.com/en-us/lib...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 (wa****@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.

Jul 11 '06 #3
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!
Jul 11 '06 #4
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 (wa****@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.

Jul 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: | last post by:
Wel, I am rebuilding the VC# 2002 project that I have deployment problems with the 2003 version, hoping this solves the problems, but now I encounter this wierd bug??? If I have the project, and...
5
by: DS | last post by:
How do I go about binding a "struct" datatype to a comboBox? When I bind it by means of the DataSource, it doesn't give any error, but the comboBox just gets populated with...
12
by: Dica | last post by:
i can't seem to find a way to cast the value in my comboBox to integer. i've tried the following: short iProjectID = Convert.ToInt16(cboProjects.SelectedValue); i keep getting a "Sepcified...
3
by: Don | last post by:
I'm getting a strange error from the following code: With Me.Combobox ' Clear list .Items.Clear() ' Add items to the list .Items.Add("item1") .Items.Add("item2")
3
by: Russ | last post by:
I have a subroutine that I call to bind a dataset to a combobox. It works sometimes. See the code below.. 1 Private Sub BindDropDownToDataSet( _ 2 ByRef control As...
10
by: ZaphodBBB | last post by:
HI I have a form with 2 comboBoxes. The first comboBox gives me a list of customers to select from. I would like to have the second comboBox populated with the names of Departments. I only want...
19
by: active | last post by:
I'm using a ComboBox to display objects of a class I've defined, say CQQ. Works great except somehow I occasionally set an Item to a String object instead of an object of type CQQ. It looks...
4
by: EManning | last post by:
I have a combobox whose rowsource is a union query. This query displays a person's name in "lastname, firstname" format and in "firstname lastname" format. The query results look like this: ...
5
by: Christian | last post by:
Hello, i have a datagrid with some columns... the last column is a ComboBox with 2 items: "yes" and "no"... i'd like when i select a value "yes" the color's column became Yello.... someone have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.