all groups > dotnet windows forms databinding > july 2005 >
You're in the

dotnet windows forms databinding

group:

ErrorProvider not picking up validation changes


ErrorProvider not picking up validation changes Rockford Lhotka
7/7/2005 12:22:13 PM
dotnet windows forms databinding:
I have a strongly-typed DataTable, with my validation code in the partial
class (behind the ColumnChanged event).

I have a single row of data bound to a detail form. That form also has an
ErrorProvider control bound to the same binding source as all the controls.

When I load the form with a new row of data, several validation rules are
broken (due to required fields). The error icons appear as desired and life
is good.

HOWEVER, when I enter data into the text boxes and tab off the error icon
remains (when it shouldn't, because the required value is now there).

I have walked through the code in the debugger so I know absolutely that my
validation logic is running and within the DataTable code I am calling

row.SetColumnError(column.ColumnName, errorText)
The errorText variable is a String, and is empty because the validation
succeeded.

BUT the error icons don't go away. It appears that the ErrorProvider control
is entirely unaware that the data values have changed, including the error
text being cleared.

The most likely possiblity I can think of is that the DataTable's
ColumnChanged event fires AFTER the UI has been notified of the property
changing. Thus any changes to data in the data source aren't reflected by
data bound controls because the UI already did its data binding updates.

Unfortunately, manually triggering the DataTable's PropertyChanged event had
no effect, so that theory isn't holding much water either...

Rocky

email: rocky at lhotka dot net



Re: ErrorProvider not picking up validation changes Beth Massi [Architect MVP]
7/8/2005 10:07:08 AM
Hi-ya Rocky,

I just tried a similar thing in VS2003 and it works fine.

Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'-....get data here... then

AddHandler MyDataset.Tables("Table1").ColumnChanged, AddressOf ValidateIt

Me.ErrorProvider1.DataSource = MyDataset
Me.ErrorProvider1.DataMember = "Table1"

Me.TextBox1.DataBindings.Add("Text", Me.MyDataset, "Table1.Field1")
End Sub

Private Sub ValidateIt(ByVal sender As Object, ByVal e As
DataColumnChangeEventArgs)
If e.Column.ColumnName = "Field1" Then
If e.ProposedValue.ToString = "This is a test" Then
e.Row.SetColumnError(e.Column, "ERROR!")
Else
e.Row.SetColumnError(e.Column, String.Empty)
End If
End If
End Sub

I have to dig out my other hard drive to try your exact scenario in VS2005,
but I would double-check to make sure you are setting up the Databindings of
the controls and the ErrorProvider to the same DataSource and DataMember.

HTH,
-B

[quoted text, click to view]

Re: ErrorProvider not picking up validation changes Beth Massi [Architect MVP]
7/8/2005 11:19:27 AM
Okay I got it working in 2005 with a partial DataTable class with validation
code. It was tricky to get the right DataSource for the ErrorProvider. I set
the Datasource property equal to the same BindingSource object as the
control (which I drag-dropped from the datasource window) and the DataMember
I left blank and it looks like that did the trick. So it looks like my
Changed events are firing properly. Maybe it's your datasource object
references are out of whack? All these new objects to bind to... this is
easier than 2003? <gd&rvvf>

-B

[quoted text, click to view]

Re: ErrorProvider not picking up validation changes Rockford Lhotka
7/8/2005 12:46:07 PM
I've played with this a bit more and am pretty convinced the events just
aren't flowing up from the data source.

For instance, I added a button to the form that directly alters a value in
the DataRow. This is not reflected on the form (TextBox or ErrorProvider).

However, if I add a line in the button click event to call
BindingSource.ResetCurrentItem - forcing rebinding of the controls - then
everything gets updated as expected (both TextBox and ErrorProvider).

As a workaround, I think I'm going to create a global handler for all
LostFocus events and just call ResetCurrentItem on each LostFocus. Not a
perfect solution (since ToolStrip buttons don't take the focus), but pretty
darn close.

Rocky

[quoted text, click to view]

Re: ErrorProvider not picking up validation changes Rockford Lhotka
7/8/2005 2:04:50 PM
Hmm, that's what I did too.

I guess maybe I'll find out how complex it is to remove and recreate a
DataSet with partial code. Perhaps something in the genned DataSet code is
messed up and recreating the thing will resolve the issue.

Rocky

[quoted text, click to view]

AddThis Social Bookmark Button