none
Combobox Woes RRS feed

  • Question

  • I had an earlier post about this, but I got it working...I added a bindingnavigator and it apparently died. I want to have a user select one combobox and through a foreign key set the default value for the next combobox. I.e. the user picks a vehicle and the next combobox defaults to the standard vehicle operator. These are two different values in two different tables....this is my code...

    Code Snippet

    ublic Class Form1
    Dim db As DataClasses1DataContext
    Dim db2 As DataClasses2DataContext
    Dim dpVehicleDB As New vehicle
    Dim dbMaint As New vehicle_mainetnance
    Dim dbOps As New vehicle_operator


    (misc stuff omitted)


    Public Sub VehicleComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VehicleComboBox.Leave


    If result = DialogResult.Yes Then
    dpVehicleDB = VehicleComboBox.SelectedItem
    NameComboBox.SelectedText = dpVehicleDB.owner
    'VehicleComboBox.SelectedText = dpVehicleDB.vehicle_key
    Me.Nameplaceholder.Text = dpVehicleDB.operator.ToString()
    Me.VehiclePlaceHolder.Text = dpVehicleDB.vehicle_key.ToString()
    Else
    Me.NameComboBox.DataSource = db.vehicle_operators
    Me.NameComboBox.DisplayMember = "name"
    Me.Nameplaceholder.Text = dpVehicleDB.operator.ToString()
    Me.VehiclePlaceHolder.Text = dpVehicleDB.vehicle_key.ToString()
    End If


    End Sub



     What's really getting me is this was working before. Now I'm getting a message stating "Unable to cast object of type 'System.Data.DataRowView' to type 'VehicleApp.vehicle'."....this is driving me nuts.


    Any ideas?


    Monday, September 8, 2008 8:55 PM

Answers

  • I would imagine you were previously binding Vehicle objects directly into the combo box's list which is why the above code worked.

    From the sound of the error message you are now instead binding a gridview to the Combo Box. I would recommend switching the combo box back to being bound directly to the results of a LINQ to SQL query instead of a combo box.

    [)amien
    Tuesday, September 9, 2008 7:02 AM
    Moderator

All replies

  • I would imagine you were previously binding Vehicle objects directly into the combo box's list which is why the above code worked.

    From the sound of the error message you are now instead binding a gridview to the Combo Box. I would recommend switching the combo box back to being bound directly to the results of a LINQ to SQL query instead of a combo box.

    [)amien
    Tuesday, September 9, 2008 7:02 AM
    Moderator
  • That makes sense since i was trying to set the source of the combobox to the same dataset as the tableadapter.

     

    I have tried switching back to using the "dataclasses1datacontext" (generic name) but when I do that, the selectedindex defaults to whatever row I have highlighted in the datagridview on the form as well....

     

     I normally don't program this poorly to the point of getting so many things crossed, and my troubles started when I added the bindingnavigator to a separate form within the same project.

     

    As for the LINQ query, are you aware of the location of any pages that I might be able to use to find out how to implement it? My last VB experience other than this project was in 2003.

    Tuesday, September 9, 2008 12:04 PM
  • Got it. I just created a different datacontext and linked it to that. It might not be efficient, but it's a simple app. The business need is pretty simple too. It should work. The new datacontext is an isntance of a datacontext with the new one being named cbvehicledb.

     

    Code Snippet
       cbvehicleDB = VehicleComboBox.SelectedItem
                NameComboBox.SelectedText = cbvehicleDB.cbowner

     

     

     

    Tuesday, September 9, 2008 1:05 PM