Changing Bindingsource.datasource at RuntimeChanging Bindingsource.datasource at Runtime
Old forum URL: forums.lhotka.net/forums/t/5494.aspx
pandelaras posted on Tuesday, September 30, 2008
I am using csla 3.04 and i am trying to change a a bindingsource.datasource at a windows forms grid. The datasource changes when the user selects a different client(person) from the combobox (a new list is created) . I am also saving a copy of the object, in order to databind itself to a combobox in the grid later on, but that causes no problem. The problem is when i use RebindUI(False, True) the datagridview shows no change although the datasource changed! I have to select the same item on the combobox twice to see a change in the grid! What am i doing wrong? Here is the code
COMBOBOX event
Private Sub Baseis_pelaton_GComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Baseis_pelaton_GComboBox.SelectedIndexChanged
If PROTI_FORA = False Then
If Baseis_pelaton_GBindingSource.Current IsNot Nothing Then
'creating new object from combobox choice works ok
Dim nea_eidh As EIDH_PARAST_ANA_PEL_ERL = EIDH_PARAST_ANA_PEL_ERL.GetEIDH_PARAST_ANA_PEL_ERL(DirectCast(Baseis_pelaton_GBindingSource.Current, Bash_pelaton_G).Id)
'saving it to the form
m_EIDH_PARAST = nea_eidh.Clone
'saving old copy to the second bindingsource; this is used to bind itself to a grid's combobox later on
EIDHPARASTANAPELERLBindingSource.DataSource = nea_eidh
' no saving just refreshing UI
RebindUI(False, True)
End If
End If
End Sub
REBIND CODE
Private Sub RebindUI(ByVal saveObject As Boolean, ByVal rebind As Boolean)
Me.EIDH_PARAST_ANA_PEL_ERLBindingSource.RaiseListChangedEvents = False
Try
' unbind the UI
UnbindBindingSource(Me.EIDH_PARAST_ANA_PEL_ERLBindingSource, saveObject, False)
' save or cancel changes
If saveObject Then
m_EIDH_PARAST.ApplyEdit()
Try
Dim temp1 As EIDH_PARAST_ANA_PEL_ERL = m_EIDH_PARAST.Clone()
m_EIDH_PARAST = temp1.Save()
Catch ex As Csla.DataPortalException
MessageBox.Show(ex.BusinessException.ToString(), _
"Error saving", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Catch ex As Exception
MessageBox.Show(ex.ToString(), _
"Error Saving", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
End Try
Else
m_EIDH_PARAST.CancelEdit()
End If
' rebind UI if requested
If rebind Then
BindUI()
End If
Finally
' ' restore events
Me.EIDH_PARAST_ANA_PEL_ERLBindingSource.RaiseListChangedEvents = True
If rebind Then
' refresh the UI if rebinding
Me.EIDH_PARAST_ANA_PEL_ERLBindingSource.ResetBindings(False)
End If
End Try
End Sub
pandelaras replied on Tuesday, September 30, 2008
traced the error and it seems that the DirectCast(Baseis_pelaton_GBindingSource.Current, Bash_pelaton_G).Id
returns the previous and not the current id at first choice.
SOLUTION
DO NOT USE THIS EVENT :)
Baseis_pelaton_GComboBox.SelectedIndexChanged
use this
Baseis_pelaton_GBindingSource.CurrentItemChanged
Copyright (c) Marimer LLC