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
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