Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Data-Binding
I appreciate if anyone can advise if there is event handler or other ways to validate changes from a data binded textbox before updating data source. I have a windows form with a data repeater. In the data repeater, I have a textbox which is data binded as follows:
BindingSource bindingsource = new BindingSource();
DataSet ds = new DataSet("Preferences"); 
 
DataTable table = new DataTable("Preference");
table.Columns.Add("Value");
 
for (int i = 1; i <= 12; i++)
 { 
   table.Rows.Add(i); 
 }
 
ds.Tables.Add(table);
 
bindingsource.DataSource = ds;
bindingsource.DataMember = "Preference";           
 
textBox1.DataBindings.Add(new Binding("Text", bindingsource, "Value", true, DataSourceUpdateMode.OnValidation)); 
 
As can be seen in the code, I have set the textbox to DataSourceUpdateMode.OnValidation. When I ran the project and change the textbox value on the form, I can see that the dataset (ds) is updated correctly. However, I want to be able to validate my entry before committing the new value into my data source.
 
Is there a special way, using handler or interface to do this please?
Posted 17-Oct-12 7:05am
YHiew360
Edited 17-Oct-12 7:06am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use the DataMemberChanged event in the BindingSource to validate your data
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 21 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100