Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a SilverLight app on ASPX with RIA services from Entity Framework backend. I have the loading of the DataGrid just fine including the related table data. I am now working on updating the fields with the combobox changes. As I debug I can see the changes on the entity object, but it does not persist. I tried calling CommitEdit on the DataGrid in the RowEditEnding event which just crashes the app with giving me the error.

Bind the CB's:
Private Sub dgStaff_PreparingCellForEdit(sender As Object, e As System.Windows.Controls.DataGridPreparingCellForEditEventArgs) Handles dgStaff.PreparingCellForEdit
   columnIndex = e.Column.DisplayIndex
   Dim cb As ComboBox = TryCast(e.EditingElement, ComboBox)
   If cb IsNot Nothing Then
     cb.DisplayMemberPath = "Description"
     Select Case columnIndex
       Case 2
         cb.SelectedItem = curPerson.Station.StationId
         cb.ItemsSource = stations
         cb.SelectedValue = curPerson.Station.StationId
       Case 3
         cb.SelectedItem = curPerson.Shift.ShiftId
         cb.ItemsSource = shifts
         cb.SelectedValue = curPerson.Shift.ShiftId
       Case 4
         cb.SelectedItem = curPerson.Team.TeamId
         cb.ItemsSource = teams
         cb.SelectedValue = curPerson.Team.TeamId
     End Select
   End If
End Sub

Get the current entity object:
Private Sub dgStaff_BeginningEdit(sender As Object, e As System.Windows.Controls.DataGridBeginningEditEventArgs) Handles dgStaff.BeginningEdit
    curPerson = CType(e.Row.DataContext, BMServiceRef.Personnel)
End Sub

Get the new values and apply them to the entity object:
Private Sub cb_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
  Dim cb As ComboBox = DirectCast(sender, ComboBox)
  Select Case columnIndex
    Case 2
      curPerson.StationId = CType(cb.SelectedItem, BMServiceRef.Station).StationId
    Case 3
      curPerson.ShiftId = CType(cb.SelectedItem, BMServiceRef.Shift).ShiftId
    Case 4
      curPerson.TeamId = CType(cb.SelectedItem, BMServiceRef.Team).TeamId
  End Select
End Sub

Let me know if I can add any more info. Thanks for looking...
Updated 20-Apr-13 8:35am

I have a workaround. In the combobox selectionChanged event I save selection to server. In the cellEditEnd event I update the dataGrid again. I would still like to get the current value to show in the combobox - the selectedIndex. If someone can help with that part(or the rest) I'll give you the solution.
The twoWay binding does not work and this is why I have to rebind the dataGrid.

  <ComboBox ItemsSource="{Binding Path=stations,Mode=TwoWay}" SelectionChanged="cb_SelectionChanged" DropDownClosed="dd_closed" />
Share this answer
You need to bind the SelectedValue
<combobox height="23">
SelectedValue="{Binding Path=buildingId, Mode=TwoWay}"
ItemsSource="{Binding Buildings}"
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900