c# - Validation on Databoud DataGridView per Column -
Then I have a datagrid view that is used to display a list of custom models. Here are some sample codes for the model:
Public class testdom {public inimenable & lt; String & gt; GetValidationErrors () {If (value1> 100; value1 & lt; = 0) yield returns "value 1 can only be between 1 and 100 (inclusive)"; If (string.IsNullOrEmpty (Value2)) yield "value 2 can not be empty"; } Public Bool ISImail {GetValidationErrors (). Count () == 0; }} Public int Value1 {get; Set; } Public String Value2 {get; Set; }}
Now suppose I was compiling the list of these models (or IEnumerable) such as
list & lt; TestModel & gt; List = helpers.GetListOfTestModels (); DataGridView1.DataSource = List;
(We can assume that the list return is valid, or it will not be stored).
Now the event occurring on the Rowlification
DataGridView, I can validate the entire line from accessing the property lines [] I can set up. Error text
, such as:
Private Zero DatagridView 1_ROValidating (Object Sender, DataGrade ViewCellCellEventErguesE) {var items = DataGrid viEW1 ROS [E. Randex]. Items in the form of database test models; If return (item == empty); If (! Item.IsValid) {dataGridView1.Rows [e.RowIndex] .ErrorText = "Failed Validation"; // Use GetValidationErrors and clamp them, but I have left it to be simple e.Cancel = true; } And {dataGridView1.Rows [e.RowIndex] .ErrorText = string.Empty; }}
Now, what I really want to do (without copying and pasting the code everywhere), validates each asset and sets How should I go about this? Maybe something with custom properties, then to get the reflection property name and use the cell in this way? Hope this all makes sense! The solution! This is an abstract feature: and such an implementation: then And something like this happens in either and with bam errors per database, database! rows [] .
[AttributeUsage (AttributeTargets.Property, AllowMultiple = true, Inherited = true)] Public abstract class validation feature: attribute {public abstract Zero valid (object value, propertyInfoFOINFO, REF ILITST; string & gt; errors); }
public class ValidStringAttribute: validation entry {#region Validation OverrideSant Public Override Validate (Object Value, PropertyInfo propertyInfo, ReferralList & Lt; string & gt; errors) {var v = propertyInfo.GetValue (value, empty); If (! String.IsNullOrEmpty (v string)) {errors.Add (string.format ("` {0} `can not be empty or empty", propertyInfo.Name);}} #endregion}
> Value2
property as follows: rowlification
or cell declaration
events:
if (! Item. IsValid) Information about property property in foreach (property). GetTrop (). GetProperties ()) {IList & lt; ValidationAttribute; ValidationAttribute;) ValidationAttribute Validation attribute;) {attribute.Validate Item, PropertyInfo, Ref List};} (list.Count & Gt; 0) Make sure this preferred browsable = propertyInfo.GetCustomAttributes (typeof (browsableAttribute) is true); If (br owsable.Count () == 0) {dataGridView1.Rows [e.RowIndex] .cel [propertyInfo.Name] .ErrorText = list [0]; }}} E.Cancel = true; }
Comments
Post a Comment