ComboBox.ValueMember Question

I have a PopulateComboBox sub, in which I assign the value member for the combo box I happen to be populating. If I create a SelectedIndexChanged sub for that combo box, and try and check the ComboBox.SelectedValue, it returns DataRowView until I reassign (within that sub) the ValueMember. Is there a reason for that that I'm perhaps missing entirely It seems somewhat pointless to me to have to do this.

Answer this question

ComboBox.ValueMember Question

  • BarKey

    That makes sense, thank you Joe. One other quick question, though:

    I have a sub that populates my combo box for me. I pass it the combo box to populate, the source (a datatable), the valuemember, as well as the displaymember. It populates just fine, except for two things:

    1: I can't set the selected index to -1 to set the initial combo box value to a blank line, due to sorting the datatable, which changes the selectedindex on me.
    2: If I don't have the displaymember and valuemember set in the calling event, it doesn't work properly. Why is that

    Code for calling PopulateComboBox:

    SGMS.PopulateComboBox(CboServerStatus, myStatusDT, "Status", "StatusID", "cboSort asc")

    Code for PopulateComboBox:

    Friend Shared Sub PopulateComboBox(ByVal Target As ComboBox, ByVal Source As DataTable, ByVal DisplayMember As String, _

    ByVal ValueMember As String)

    Try

    With Target

    .DataSource = Source

    .DisplayMember = DisplayMember

    .ValueMember = ValueMember

    End With

    Catch ex As Exception

    MessageBox.Show("PopulateComboBox1: " & ex.Message)

    End Try

    End Sub


  • B Sutton

    This is a duplicate of your other post - I've responded to the other post here:
    http://forums.microsoft.com/msdn/ShowPost.aspx PostID=63674

    Joe Stegman
    The Windows Forms Team
    Microsoft Corp.

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


  • LTaylor

    I am able to add a blank row to the dataset, so that works (thanks for the advice on that !)

    In regards to my second question, it seems to have been a glitch in my system. Before, I would set the display and value members in the code that bound the data to the combo box, but it wouldn't display properly unless I added the display and value members to the parent function (hopefully I'm using that term correctly :S). That has since cleared itself up (or I fixed whatever was causing it not to display correctly in the first place. :P)

    Thank you very much for your replies.


  • Nada Ali

    1: I can't set the selected index to -1 to set the initial combo box value to a blank line, due to sorting the datatable, which changes the selectedindex on me.

    The recommended way to handle this is to add a "blank" item to your DataTable.  If you can't do that, then the following should work:


    With Target

    .DisplayMember = DisplayMember
    .ValueMember = ValueMember
    .DataSource = Source
    .SelectedIndex = -1

    End With

     



    2: If I don't have the displaymember and valuemember set in the calling event, it doesn't work properly. Why is that

    If you don't set DisplayMember, then the ComboBox won't know to use the "Status" column for the ComboBox text (it would call ToString() on the items which will return "DataRowView").  What do you expect to happen if you don't set DisplayMember

    Joe Stegman
    The Windows Forms Team
    Microsoft Corp.

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


  • Prajakta

    Yes - SelectedValue represents the evaluation of the "ValueMember" property on the current item.  If your ComboBox is filled with Customer objects and "ValueMember" is "Name" then the SelecteValue will be Customer.Name of the currently selected customer.  If you don't set the ValueMember, then we call "ToString()" on the current item.  In your case, the current item is a DataRowView (essentially an ADO.NET DataRow) and its ToString() returns the Type name (DataRowView).

    Joe Stegman
    The Windows Forms Team
    Microsoft Corp.

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


  • Conversion

    I've implemented a function to insert a blank row into my datatable, however, I'm running into a few issues with it. Namely, it's not adding a blank entry for all the columns. Is there a way to iterate through the columns in my combobox, and based on the datatype of the column, insert the appropriate blank value

    Currently I have the following:


    Friend Shared Function InsertBlankRow(ByVal Source As DataTable) As DataTable

    Dim myNewSource As DataTable = Source

    Dim myBlankRow As DataRow = myNewSource.NewRow

    Dim i As Integer

    MessageBox.Show(myNewSource.Columns.Count)

    For i = 0 To myNewSource.Columns.Count - 1

    MessageBox.Show("DataType is: " & myNewSource.Columns(i).ToString)

    'If (myNewSource.Columns(i).DataType() = Odbc.OdbcType.DateTime) Then

    ' MessageBox.Show("Datatype is datetime!")

    'End If

    myBlankRow(i) = ""

    Next

    myNewSource.Rows.InsertAt(myBlankRow, 0)

    Return myNewSource

    End Function


     


    The problem with this is that it will error out if I try and pass it a datatable that has a datetime column in it. How can I account for that (You can see that I've tried to check for this and failed. :S)

  • ComboBox.ValueMember Question