473,396 Members | 1,777 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Data Binding and Event-Driven Validation

1
I am having trouble preventing a bound source from updating using event-driven validation.

There is a very nice article on MSDN explaining this;
http://msdn.microsoft.com/en-us/library/ms229603.aspx

It clearly states, "When you use data binding, the data in your control is synchronized with the data source during execution of the Validating event. If you cancel the Validating event, the data will not be synchronized with the data source."

So I've created a tiny example application called NoSourceUpdate, which basically contains a list of some data, an ID and a Name. I create the list, and bind it to a listbox, then bind the textboxes to the list's properties. There are also 2 lables bound to the list to reflect the internal list's values to demonstate the problem I'm having.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private errorlist As New ErrorProvider
  3.  
  4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  5.         Dim DataList As New List(Of Data)
  6.  
  7.         Dim NewData As New Data
  8.         NewData.ID = 1
  9.         NewData.name = "Name One"
  10.         DataList.Add(NewData)
  11.  
  12.         NewData = New Data
  13.         NewData.ID = 2
  14.         NewData.name = "Name Two"
  15.         DataList.Add(NewData)
  16.  
  17.         Dim ListBinder As New BindingSource
  18.         ListBinder.DataSource = DataList
  19.  
  20.         ListBox1.DataSource = ListBinder
  21.         ListBox1.DisplayMember = "Name"
  22.  
  23.         TextBox1.DataBindings.Add("Text", ListBinder, "Name")
  24.         TextBox2.DataBindings.Add("Text", ListBinder, "ID")
  25.  
  26.         Label1.DataBindings.Add("Text", ListBinder, "Name")
  27.         Label2.DataBindings.Add("Text", ListBinder, "ID")
  28.     End Sub
  29.  
  30.     Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
  31.         If (sender.text.length < 5) Then
  32.             e.Cancel = True
  33.             errorlist.SetError(sender, "Name is too short!")
  34.         Else
  35.             errorlist.SetError(sender, "")
  36.         End If
  37.     End Sub
  38. End Class
  39.  
  40. Public Class Data
  41.     Private myName As String
  42.  
  43.     Public Property name() As String
  44.         Get
  45.             Return myName
  46.         End Get
  47.         Set(ByVal value As String)
  48.             myName = value
  49.         End Set
  50.     End Property
  51.  
  52.     Private myID As Integer
  53.  
  54.     Public Property ID() As Integer
  55.         Get
  56.             Return myID
  57.         End Get
  58.         Set(ByVal value As Integer)
  59.             myID = value
  60.         End Set
  61.     End Property
  62. End Class
  63.  
When I enter a name into the name textbox that fails the validating event, the invalid input is still syncronized to the bound data source. This is verified by the changes being propigated to the lisbox's display member, and also to the name lable.

You can see a screenshot of what is happening here;
http://imagebin.org/index.php?mode=image&id=26163

Clearly I am misunderstanding how this works. Canceling the validating event traps the users focus on the invalid textbox, which is great. It also stops the validated event from firing, also great. However, the invalid data is still syncronized to the data source which is causing all sorts of exceptions being thrown. Sure I could code another level of validation into the data class, but then why does the validating event exist? Why is the data still being syncronized when I cancle the validating event?

Can somebody please explain to me what I am doing wrong, or at least show me how to do this properly?

Thanks!
Diff
Sep 10 '08 #1
0 1265

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Marcin Floryan | last post by:
Hello! My question regards opening (and re-opening) Form and the Load event. I have a main form (frmMain) and I also have a data form (frmData). In the main form I have created: Private...
0
by: Ann Morris | last post by:
INTRODUCTION One of the most powerful aspects of .NET and Windows Forms is data binding. Data binding is the process of associating user interface (UI) elements with a data source to generate a...
3
by: vinayak | last post by:
Hi I am displaying data in Datagrid in ASP.NET with Edit/Update functionality for each row. On the same page I have 2 Button controls which submits the request to server. These button controls...
2
by: Andrew Robinson | last post by:
Is there any way to accomplish two way data binding in a Details View with a DataSet or DataTable as the DataSource. All I want is to get an updated DataSet or DataTable back from the...
2
by: Tubs | last post by:
I am trying to use data binding to avoid having to write event handlers everywhere for my data transfer from controls to data source and all is working well except, i have a condition i would like...
19
by: Simon Verona | last post by:
I'm not sure if I'm going down the correct route... I have a class which exposes a number of properties of an object (in this case the object represents a customer). Can I then use this...
0
by: Larry Serflaten | last post by:
I am not sure how many are aware of this sort of data binding, but as it is new to many (classic) VB developers I thought I would post this once just to let people know of its availablility. ...
0
by: mjsterz | last post by:
I've been working with VB .NET for less than a year and this is the first time I've posted on one of these groups, so let me apologize beforehand if I'm being unclear, not posting my issue...
9
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New...
1
by: bogdan | last post by:
I need to execute some code _after_ page controls are bound to data (e.g. DropDownList). I could probably handle DataBound events for each control. But if I wanted to place the code in a page...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.