GrapeCity SPREAD for WPF 2.0J
ValidateAll メソッド

ValidateAll メソッドを使用すると、コントロールに設定されたすべての検証を任意のタイミングで実行できます。ValidateAll メソッドでは、データソースに実装された検証、DataAnnotation による検証、入力規則が実行の対象となります。コントロールがデータ連結されているかどうかに関わらず使用できます。

ValidateAll メソッドを実行すると、コントロールはすべての不正なセルおよび行のエラーを表示します。

ValidateAll メソッド

次のサンプルコードはボタンが押されたときに ValidateAll メソッドを実行します。このサンプルコードでは、コントロールを入力違反のデータを含む EmployeeWithErrorsCollection に連結しています。ボタンをクリックすると不正なセルおよび行にエラーが表示されます。

XAML
コードのコピー
<Button Name="BtnValidateAll" Content="検証を実行" Click="BtnValidateAll_Click"/>
          :
<sg:GcSpreadGrid Name="gcSpreadGrid1" ItemsSource="{StaticResource EmployeeWithErrorsCollection}"/>
C#
コードのコピー
private void BtnValidateAll_Click(object sender, RoutedEventArgs e)
{
    SpreadValidationError[] errlist = gcSpreadGrid1.ValidateAll();
}
Visual Basic
コードのコピー
Private Sub BtnValidateAll_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim errlist As SpreadValidationError() = gcSpreadGrid1.ValidateAll()
End Sub
Employee および EmployeeWithErrorsCollection クラス(C#)
C#
コードのコピー
[CustomValidation(typeof(EmployeeValidator), "IsGrownUp")]
public class Employee : INotifyPropertyChanged
{
    string id;
    string name;      
    DateTime hiredate;
    DateTime birthday;
    [Required(ErrorMessage = "IDは必ず入力してください。")]
    public string ID
    {
        set { id = value; NotifyPropertyChanged("ID");}
        get { return id; }
    }
    [Display(ShortName="氏名")]       
    public string Name
    {
        set { name = value; NotifyPropertyChanged("Name"); }
        get { return name; }
    }
    [Display(ShortName = "入社日")]
    public DateTime HireDate
    {
        set
        {
            hiredate = value; NotifyPropertyChanged("HireDate");
        }
        get { return hiredate; }
    }
    [Display(ShortName = "誕生日")]
    public DateTime Birthday
    {
        set { birthday = value; NotifyPropertyChanged("Birthday"); }
        get { return birthday; }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
public class EmployeeWithErrorsCollection : ObservableCollection<Employee>
{
    public EmployeeWithErrorsCollection()
    {
        Add(new Employee() { ID = "U10200115", Name = "福山 敏道", HireDate = DateTime.Today, Birthday = DateTime.Today });
        Add(new Employee() { ID = "", Name = "", HireDate = DateTime.Today, Birthday = DateTime.Today.AddYears(-20) });
    }
}
Employee および EmployeeWithErrorsCollection クラス(Visual Basic)
Visual Basic
コードのコピー
<CustomValidation(GetType(EmployeeValidator), "IsGrownUp")> _
Public Class Employee
    Implements INotifyPropertyChanged
    Private m_id As String
    Private m_name As String
    Private m_hiredate As DateTime
    Private m_birthday As DateTime
    <Required(ErrorMessage:="IDは必ず入力してください。")>
    Public Property ID() As String
        Get
            Return m_id
        End Get
        Set(value As String)
            m_id = value
            NotifyPropertyChanged("ID")
        End Set
    End Property
    <Display(ShortName:="氏名")> _
    Public Property Name() As String
        Get
            Return m_name
        End Get
        Set(value As String)
            m_name = value
            NotifyPropertyChanged("Name")
        End Set
    End Property
    <Display(ShortName:="入社日")> _
    Public Property HireDate() As DateTime
        Get
            Return m_hiredate
        End Get
        Set(value As DateTime)
            m_hiredate = value
            NotifyPropertyChanged("HireDate")
        End Set
    End Property
    <Display(ShortName:="誕生日")> _
    Public Property Birthday() As DateTime
        Get
            Return m_birthday
        End Get
        Set(value As DateTime)
            m_birthday = value
            NotifyPropertyChanged("Birthday")
        End Set
    End Property
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Protected Sub NotifyPropertyChanged(propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
End Class
Public Class EmployeeValidator
    Public Shared Function IsGrownUp(employee As Employee) As ValidationResult
        Dim result As ValidationResult = Nothing
        If employee.Birthday.AddYears(20).CompareTo(employee.HireDate) = 1 Then
            result = New ValidationResult("20歳以上の方しか勤務できません。", New String() {"Birthday", "HireDate"})
        End If
        Return result
    End Function
End Class
Public Class EmployeeWithErrorsCollection
    Inherits ObservableCollection(Of Employee)
    Public Sub New()
        Add(New Employee() With {.ID = "U10200115", .Name = "福山 敏道", .HireDate = DateTime.Today, .Birthday = DateTime.Today})
        Add(New Employee() With {.ID = "", .Name = "", .HireDate = DateTime.Today, .Birthday = DateTime.Today.AddYears(-20)})

    End Sub
End Class
関連トピック

 

 


Copyright © 2012 GrapeCity inc. All rights reserved.