DataBound Controls Selecting Text Inadvertently

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
this one is strange, and frankly I don't know if anyone will be able to help given the complexity of the form and situation, but I'll try to describe it as best I can.

I have multiple (7) binding sources, each bound to their own table in a dataset.
Bind_TblList: Is simply bound to the list of tables and is the datasource of a combobox. When that combobox changes selection, I switch my current "view" to the newly selected table.

Bind_Tbls: Is the datasource of a DGV, and when the "View" changes I set the its datasource to the appropriate "table bound" bindingsource.

there is no need to list them all but lets just say two of the binding sources are
Bind_Cust
Bind_Orders

Each Table has a "TabPage" on a TabControl that contains the "Details" view for each Table. On the Orders detail view there is a ComboBox that points back to the Owning Customer, which displays the "name" for the "id" (simply valuemember versus Displaymember). I soon found that when viewing things on the Customer Detail View, switching between records, it updated the Bind_Cust bindingsource, and thus the "values" of the OrderCustomerComboBox were changing, due to the OrderCustomerComboBox's DataSource being set to the same Bind_Cust. Realizing my folly, I thought on how I would resolve this issue, because if I change the Customer Table in the Customer "View" the dataset believes that there have been changes made and effectively updates the Orders Table along with the Customer Table change. Not that I changed the Orders table, but it "thinks" I did because the OrderCustomerComboBox's DataSource changed, and being DataBound to the Orders.CustID field it assigned the value on the end edit. Rather Annoying.

I thought, to myself, that since the OrderCustomerComboBox's DataSource is only there for an "Item List" it had no need of a BindingSource and could be pointed directly to the table.
VB.NET:
[u]Old Way[/u]
Bind_cust.DataSource = myDataSet
Bind_Cust.Datamember = "CustTable"

Bind_Order.DataSource = myDataSet
Bind_Order.Datamember = "OrderTable"

OrderCustomerCombBox.DataSource = bind_cust
OrderCustomerCombBox.DisplayMember = "name"
OrderCustomerCombBox.ValueMember = "id"

[u]New Way[/u]
OrderCustomerCombBox.DataSource = myDataSet.CustTable
OrderCustomerCombBox.DisplayMember = "name"
OrderCustomerCombBox.ValueMember = "id"

This worked on the front that no matter which element I select in the Customer View, the Order View is not affected with regards to the OrderCustomerComboBox, and there are no more data glitches.

However, when viewing the Customer View and through the combobox switch to the OrdersView an interesting thing happens. The SelectView() method tells the "DetailsViewTabControl" to "select" the appropriate TabPage, which in this case is TabOrders, but when it does this, the first control on the TabPage (OrderIDTextBox) is Activated and thus receives the focus, however, the OrderCustomerComboBox's Text is Completely Selected. I deselect all the text and switch back to the Customer View and then back to the Order View and the ComboBox is selected again, even though the focus is in the ID TextBox prior to it in the TabOrder.

Any Idea why using a direct link to the "DataTable" would cause this effect on the selection of a control's text when switching between different Tab Pages Programmatically?

Thanks
 
You started to lose me a bit there but I'm guessing the problem is that you have two BindingSources bound to the same DataTable and when you select a record in a control bound to one of those BindingSources the other one changes its selection too. Is that correct?

If so, the solution is to bind no more than one BindingSource to the DataTable itself. For all, or all but one, of the BindingSources, create a new DataView from the DataTable and bind that to the BindingSource.
 
You started to lose me a bit there but I'm guessing the problem is that you have two BindingSources bound to the same DataTable and when you select a record in a control bound to one of those BindingSources the other one changes its selection too. Is that correct?
No. I understood why that happened. I was using the same bindingsource as a "DataSource" and as a "DataBinding", which works in a "Detals View", however, I was using it as a "Secondary DataSource" to a different Details View. I've fixed that. My issue is more on the Windows Forms, not the Data Access, because my 'controls' are Highlighting their text when they shouldn't be.

THis Image is part of my initial screen. Note the ComboBox on the Navigator Strip, that is my control to switch views. You can see there appears to be two DGV's on the left, which are bound to child tables of the Customers table in a Split Container.
rc_cust.jpg


This is only a fraction of the full thing to keep image size (dimensions) down, but it is the important fraction. NOw when I go to the ComboBox I've indicated, I can select which "table" i wish to view:
rc_cust_to_bol.jpg


And when I click on BOL (Bill of Lading) it switches views just perfectly and this happens:
rc_bol.jpg


As you can see the controls labeled in red, (1,2,3) are actually comboboxes (though they stretch past the clip of the image). and their text is all highlighted, even though the top TextBox control has the focus.

Now, ComboBox labeled #3 is the Customer ComboBox, linking the BOL to the Customer table in a Foreign Key relationship, however the Value of that field in the table is the same as Cust Id field of the Customers view, but I'm displaying the Customer Name. This is achieved through the DataSource/Display|Value Member Properties of the ComboBox. However, the DataBinding is set to the BindingSource for the BOL table.

The initial binding setup occurs at connection, binding all my BindingSources to the loaded Dataset, but again we are dealing with like 3 List Tables, 3 Data Tables, 2 Views, each with their own BindingSource.

Initially I had the Bind_Cust binding source working as the DataBinding for the Customer View, and as the DataSource for the BOL View's Customer ComboBox. That was my initial problem, because when the customer view changed, it intrinsically chanced the Position of the bind_cust and thus the value of the Customer ComboBox on the BOL view. It never recognized the glitch in my interface because the DataBindings were set to be on "Validation", and I never entered that combo box for its value to need to be "validated". however, when I changed something in the Customer view, the "EndEdit" called on the other bindsources changed the value in the BOL table because the Bind_cust position was different. Now, this part I have to a degree solved, by intituting extra binding sources whose sole purpose is to be DataSources for those controls.
(I am Working on another additional method to handling when my Bindings are latched to the controls and when they aren't. A Suspend/Resume based upon current view, which I think may help with future problems as well.)

My current problem I'm asking help with at the moment, is that Now that my "CustomerCombBox" has a Unique datasource bindingsource attached (instead of using the aforementioned bind_cust) it now selects the text in the control every time I switch views. (Those three pictures can be reversed, I can go to each control on the BOL view, de-highlight the text, go to the combobox and select Customer, and then Select BOL and they are selected again).
<edit>
I did manage to fix the glitch by setting all my comboboxes to DropDownList, but it still is a curious glitch I'd like to understand...understanding would lead to not causing to occur in the future. :)

Thanks
 
Last edited:
Back
Top