combobox gives unexpected cast error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin

    #1

    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.EventArg s) Handles MyBase.Load
    FillCombo()
    End Sub

    Public Sub FillCombo()
    ComboBox1.DataS ource = MyTable
    ComboBox1.Value Member = "ID"
    ComboBox1.Displ ayMember = "Name"
    End Sub

    Private Sub ComboBox1_Selec tedIndexChanged (ByVal sender As
    System.Object, ByVal e As System.EventArg s) Handles
    ComboBox1.Selec tedIndexChanged
    Dim val As Integer
    Dim row As DataRowView

    If ComboBox1.Selec tedIndex -1 Then
    *** val = CInt(ComboBox1. SelectedValue)
    row = DirectCast(Comb oBox1.SelectedI tem, 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.Value Member = "ID"
    ComboBox1.Displ ayMember = "Name"
    ComboBox1.DataS ource = 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_Window s_Forms_ListCon trol_ValueMembe r.htm

    Martin

    --
    It''s Software Jim, but not as we know it!
  • Jared Parsons [MSFT]

    #2
    Re: combobox gives unexpected cast error

    Hello Martin,
    If I rewrite this (setting DataSource as the last line), it works as
    expected.
    >
    Public Sub FillCombo()
    ComboBox1.Value Member = "ID"
    ComboBox1.Displ ayMember = "Name"
    ComboBox1.DataS ource = 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]
    jaredpar@online .microsoft.com
    All opinions are my own. All content is provided "AS IS" with no warranties,
    and confers no rights.


    Comment

    • Walter Wang [MSFT]

      #3
      RE: combobox gives unexpected cast error

      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 SelectedValueCh anged 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 (wawang@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.

      Comment

      • Martin

        #4
        RE: combobox gives unexpected cast error

        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!


        Comment

        • Walter Wang [MSFT]

          #5
          RE: combobox gives unexpected cast error

          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 (wawang@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.

          Comment

          Working...