Click here to Skip to main content
15,897,090 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Rich Text Box Pin
Dave Kreskowiak17-Jan-12 2:11
mveDave Kreskowiak17-Jan-12 2:11 
GeneralRe: Rich Text Box Pin
Fred Andres17-Jan-12 7:35
Fred Andres17-Jan-12 7:35 
AnswerRe: Rich Text Box Pin
Shameel19-Jan-12 19:03
professionalShameel19-Jan-12 19:03 
QuestionVB Scripting for OUTLOOK 2003 Pin
Yogesh M kumar14-Jan-12 19:09
Yogesh M kumar14-Jan-12 19:09 
QuestionContains Method returns false if string and parameter are equal? Pin
Dominick Marciano14-Jan-12 16:15
professionalDominick Marciano14-Jan-12 16:15 
AnswerRe: Contains Method returns false if string and parameter are equal? Pin
Dave Kreskowiak15-Jan-12 3:46
mveDave Kreskowiak15-Jan-12 3:46 
GeneralRe: Contains Method returns false if string and parameter are equal? Pin
Shameel19-Jan-12 19:07
professionalShameel19-Jan-12 19:07 
QuestionDatabound combobox filtering issue Pin
Stefano Leoni13-Jan-12 12:29
Stefano Leoni13-Jan-12 12:29 
I'm using VB Express 2010.

This is my scenario: a form shows records one by one from a datatable via a bindingsource. One of the field is showed in a combobox whose items come from a second datatable (lookup datatable), defined in the same dataset of the first one and with a datarelation between them.

So I have a second bindingsource with datasource set to the dataset and datamember set to lookup datatable; this bindingsource is the datasource of the combobox.
Now, I don't want all the records from the lookup datatable to be showed in the combobox everytime, but only a subset which comes from filtering the lookup bindingsource accordingly to other selections the user make in the form. There are mainly two different situation to be managed to reach this behavior:

- when the user changes other fields relevant for filtering combobox's items during record editing
- when a different record is selected changing main bindingsource position.

I wrote the following code in order to reach the goal:

Private FilterComboBoxDataSource()
    ' Variable to store actual SelectedItem from LookUp BindingSource
       Dim t As DataRowView = Nothing

    ' Variable to store actual ItemID value from Main BindingSource
   Dim i as Integer = -1

   ' Stores the current row from Main BindingSource as a typed DataRow from Main DataTable
    Dim r As MainRow = CType(CType(Me.MainBindingSource.Current, DataRowView).Row, MainRow)

    ' Stores actual UpdateMode for every Binding of the ComboBox DataBinding Collection
    ' and changes it to DataSourceUpdateMode.Never
    ' (At moment only one binding exists, that for SelectedValue)
       Dim updateModes As Dictionary(Of BindingMemberInfo, DataSourceUpdateMode) =
           New Dictionary(Of BindingMemberInfo, DataSourceUpdateMode)
       For Each b As Binding In Me.ItemIDComboBox.DataBindings
           updateModes.Add(b.BindingMemberInfo, b.DataSourceUpdateMode)
           b.DataSourceUpdateMode = DataSourceUpdateMode.Never
       Next

   ' If ItemID isn't Null, searches related record in LookUp BindingSource underlying DataTable and stores it
   ' (Should use IsNull Function of untyped DataRow because ItemID is a NonNull Field)
       If Not r.IsNull("ItemID") Then
           Dim dv As DataView = CType(Me.LookUpBindingSource.List, DataView).Table.DefaultView
           dv.Sort = "ID ASC"
           t = dv(dv.Find(r.ItemID))
       ' Stores ItemID value - needed because t couldn't be find, so ItemID will be used to relocate the record - See below
      i = r.ItemID
       End If

   ' Set up Filter for LookUp BindingSource
   ' Field1, Field2 and Field3 are fields from Main DataTable whose values are displayed, via Main BindingSource,
   ' in three CheckBoxes on the form
       Me.LookUpBindingSource.Filter =
           String.Format("Field1 = {0} And Field2 = {1} And Field3 = {2}",
                         Me.Field1CheckBox.Checked,
                         Me.Field2CheckBox.Checked,
                         Me.Field3CheckBox.Checked)

   ' If an item was previously selected in ComboBox (t is not nothing)
   ' tries to find and reselect it in the ComboBox by setting the position
   ' of filtered LookUp BindingSource
   ' IT DOESN'T WORK: IndexOf(t) always returns -1. Why?
       If Not IsNothing(t) Then
           Me.TipiMovimentoBindingSource.Position =
               Me.TipiMovimentoBindingSource.IndexOf(t)
       End If

   ' If an item was previously selected in ComboBox (i > -1)
   ' tries to find and reselect it in the ComboBox by setting the position
   ' of filtered LookUp BindingSource
   ' THIS WORKS
       If i > -1 Then
           Me.TipiMovimentoBindingSource.Position =
               Me.TipiMovimentoBindingSource.Find("ID", i)
       End If

   ' Reset DataSourceUpdateMode for each binding of ComboBox
       For Each b As Binding In Me.IDTipoComboBox.DataBindings
           b.DataSourceUpdateMode = updateModes(b.BindingMemberInfo)
       Next

       updateModes = Nothing
   End Sub


This Sub is called from the CheckedChanged event handlers of field1, field2 and field3 checkboxes, which are the fields the filter is based on, and accomplish filtering caused by user interaction on these field; everything works fine.

The sub is also called from main BindingSource.CurrentChanged event handler, so that the combobox shows only those items related to actual values of field1, field2 and field3 when a different record become the current one, and here is where the issue arises.

Given that the binding to the main bindingsource for SelectedValue Property of the combobox has DataSourceUpdateMode set to OnPropertyChanged, as I need an immediate push back of the selected value, I first switch it to Never, store the current value for the field from the main source, filter the lookup datasource, try to locate the correct item basing on ItemID value in main source and, finally, turn back DataSourceUpdateMode to previuos setting. This way I should have no changes in the record due to filtering operation, and in fact at the end of the Sub the row related to the record is Unchanged and dataset HasChanges returns false.

However, when the position in the main bindingsource changes, it turns out that the row is modified in the ItemID field, the one bound to the combo box, though its value is the same as the original one, and dataset HasChanges returns true.

This occurs not for all records, but only when the new current record has a different combination of field1, field2 and field3 values than the previous one. So it seems that when main BindingSource position changes, it happens something like this:

- combo box items are those filtered for previous record basing on its field1, field2 and field3 values
- if the combination of field1, field2 and field3 values is the same for new current record, IDItem from main BindingSource finds a correspondance in the item list of the combobox
- however, if the combination of these fields is different, IDItem couldn't find a correspondance. At this point, lookup BindingSource Position doesn't reflect IDItem value in main BindingSource
- next, main BindingSource.CurrentChanged event is triggered and FilterComboBoxDataSource is invoked
- here is performed the filtering for new values and lookup BindingSource Position is set to the item corresponding to IDItem value in main BindingSource

If this is what appears to happens, how can I intercept changes in main BindingSource position so that I can filter lookup ComboBox before the newly selected record becomes the current one?

On the other side, given that the record is unmodified before and after the call to FilterComboBoxDataSource, it seems tha program flow is correct. Why the record is modified when the position in main BindingSource changes? It seems that it's a problem of the framework that sees a changes in ItemID field during filtering, ignoring the setting of DataSourceUpdateMode to Never.

At the end I've got a record which appear to be changed, but it's not true and for me is a problem because I want to track only changed records.

Any idea about how to solve this problem?

Thanks in advance

Stefano
AnswerRe: Databound combobox filtering issue Pin
Stefano Leoni17-Jan-12 3:17
Stefano Leoni17-Jan-12 3:17 
Questiondrop down list selected value Pin
Seema Bawa13-Jan-12 11:11
Seema Bawa13-Jan-12 11:11 
QuestionRe: drop down list selected value Pin
Richard MacCutchan13-Jan-12 22:31
mveRichard MacCutchan13-Jan-12 22:31 
AnswerRe: drop down list selected value Pin
sri08018818-Jan-12 23:44
sri08018818-Jan-12 23:44 
QuestionOpinions needed on "shared" keyword Pin
nlarson1112-Jan-12 8:05
nlarson1112-Jan-12 8:05 
AnswerRe: Opinions needed on "shared" keyword Pin
Luc Pattyn12-Jan-12 8:16
sitebuilderLuc Pattyn12-Jan-12 8:16 
GeneralRe: Opinions needed on "shared" keyword Pin
nlarson1112-Jan-12 8:21
nlarson1112-Jan-12 8:21 
GeneralRe: Opinions needed on "shared" keyword Pin
Dave Kreskowiak12-Jan-12 8:32
mveDave Kreskowiak12-Jan-12 8:32 
GeneralRe: Opinions needed on "shared" keyword Pin
nlarson1112-Jan-12 8:38
nlarson1112-Jan-12 8:38 
GeneralRe: Opinions needed on "shared" keyword Pin
Dave Kreskowiak12-Jan-12 9:38
mveDave Kreskowiak12-Jan-12 9:38 
GeneralRe: Opinions needed on "shared" keyword Pin
Luc Pattyn12-Jan-12 8:34
sitebuilderLuc Pattyn12-Jan-12 8:34 
GeneralRe: Opinions needed on "shared" keyword Pin
nlarson1112-Jan-12 8:51
nlarson1112-Jan-12 8:51 
AnswerRe: Opinions needed on "shared" keyword Pin
Eddy Vluggen12-Jan-12 10:40
professionalEddy Vluggen12-Jan-12 10:40 
GeneralRe: Opinions needed on "shared" keyword Pin
nlarson1113-Jan-12 2:44
nlarson1113-Jan-12 2:44 
GeneralRe: Opinions needed on "shared" keyword Pin
Eddy Vluggen13-Jan-12 6:34
professionalEddy Vluggen13-Jan-12 6:34 
QuestionDynamic dropdowns enable without post back Pin
byka12-Jan-12 6:31
byka12-Jan-12 6:31 
AnswerRe: Dynamic dropdowns enable without post back Pin
DannyStaten12-Jan-12 10:39
DannyStaten12-Jan-12 10:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.