User control event dilemma

Joined
Sep 13, 2005
Messages
15
Programming Experience
1-3
I have what is perhaps a bone-headed question.

I am working on a small application where

- I have a dataset filled with stuff, in two tables

- I have a base form, in which there is a panel

- At runtime, the panel is filled with repeated user controls for each record of one table in the dataset

- Inside the user control there is a combo box, which is bound to a column in a table in the dataset, and is populated with valuemembers and displaymembers from another table

Standard stuff, and it all works fabuloso, BUT:

I am having some problem with events -- I would like the base form to "see" an event any time any row in the dataset is modified. Problem is, it seems that because the dataset is being modified by a user control inside the panel, the base form never sees an event generated by the changes to the dataset.

Do I have to manually "bubble" rowchanged events from the user control up to the base form? If so, how?
 
Look at the help topic for the DataTable class itself and you'll see that it has events, so you can handle those events in your form rather than events of the controls.
 
Thanks -- if I understand you, I think that's what I am doing, though, and it's not working. I have an event handler in the main form that is set to handle the table's dataRowChangeEvent. But it acts as if it does not "see" the rowchange events in cases where the table is modified specifically by this user control. It's very odd.

This is the generall structure I have in the main form:

VB.NET:
AddHandler mytable.RowChanged, AddressOf rowChangedHandler
and
VB.NET:
Private Sub rowChangedHandler(ByVal sender As Object, ByVal e As DataRowChangeEventArgs)

' Do things (I have working code here)

End Sub
Is that what you are suggesting?

This setup works when the dataset is modified by other means (other controls, for example), just not for my user control specifically. And the really odd thing is that the changes made by the control DO persist in the dataset, so it is making the edits. The event for the change just is not firing or is not visible to the main form.
 
Hey Merrill,

This does sound kinda weird. Does the event get fired within your usercontrol? If so declare a new public event in the usercontrol:-

public event MyRowChangedEvent()

Match the signature of the original event if necessary. Then fire this event on the original event:

RaiseEvent MyRowChangedEvent

Which you can Add a handler for in the main form.

However if the usercontrol is using the same dataset I don't understand why it isn't picking it up to begin with.
 
New info on this problem: with some dubug messages added in my code, I can see now what's happening:

When the offending combobox is changed to a new value, for some reason that change is not setting the rowstate of the row in the dataview to which it is bound immediately, as I would expect it to. So the according to the dataview, nothing has changed, even though the value selected in the combobox is different.

BUT, if I then move focus to another control that displays the data, a datagrid in this case, whacky things happen: the change initiated from the combobox will take effect, but only after I click on the matching row in the grid. At this point, the row goes "modified" and all events fire, &c. It's like the change from the combobox is waiting around for something to make it commit.

So what I am not understanding is how a changed to the selected value in the combo box can be made to "commit" right when it's made. Tried .endedit() in a handler of combobox.selectionChangeCommitted, but so far no joy.

Still puzzled, but it feels like I'm closer to the answer...
 
OK, closer still; got endedit() to help some:

Adding this event handler makes the other controls see the change to the data once the combo is validated:

VB.NET:
Private Sub myCB(ByVal sender As Object, ByVal e As System.EventArgs) HandlesmyCB.Validated
		Debug.WriteLine("Combo Validated")
		CType(Me.BindingContext.Item(mydepts).Current, DataRowView).EndEdit()
End Sub

However, I now find that the problem is a focus issue: the combobox is not validated and it's changes commited until focus leaves the combobox (usual pattern), which in my case is a problem because I need to have that happen immediately upon a change being made.

Does anyone know a good pattern for a combo box where validation is caused to happen immediately upon a change to the selected value? e.g. User click > validation > commit changes all before focus leaves the control.
 
Back
Top