■ IDXDataErrorInfo 인터페이스를 사용해 데이터를 검증하는 방법을 보여준다.
▶ Task.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
using System; using DevExpress.XtraEditors.DXErrorProvider; namespace TestProject { /// <summary> /// 작업 /// </summary> public class Task : IDXDataErrorInfo { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 작업명 - TaskName /// <summary> /// 작업명 /// </summary> public string TaskName { get; set; } #endregion #region 시작 일자 - StartDate /// <summary> /// 시작 일자 /// </summary> public DateTime StartDate { get; set; } #endregion #region 종료 일자 - EndDate /// <summary> /// 종료 일자 /// </summary> public DateTime EndDate { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public // IDXDataErrorInfo #region 에러 구하기 - GetError(errorInfo) /// <summary> /// 에러 구하기 /// </summary> /// <param name="errorInfo">ErrorInfo 객체</param> public void GetError(ErrorInfo errorInfo) { if(StartDate > EndDate) { SetErrorInfo(errorInfo, "Either StartDate or EndDate should be corrected.", ErrorType.Critical); } } #endregion // IDXDataErrorInfo #region 속성 에러 구하기 - GetPropertyError(propertyName, errorInfo) /// <summary> /// 속성 에러 구하기 /// </summary> /// <param name="propertyName">속성명</param> /// <param name="errorInfo">ErrorInfo 객체</param> public void GetPropertyError(string propertyName, ErrorInfo errorInfo) { switch(propertyName) { case "StartDate" : if(StartDate > EndDate) { SetErrorInfo(errorInfo, "StartDate must be less than EndDate", ErrorType.Critical); } break; case "EndDate" : if(StartDate > EndDate) { SetErrorInfo(errorInfo, "EndDate must be greater than StartDate", ErrorType.Critical); } break; case "TaskName" : if(IsStringEmpty(TaskName)) { SetErrorInfo(errorInfo, "Task name hasn't been entered", ErrorType.Information); } break; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region ErrorInfo 객체 설정하기 - SetErrorInfo(errorInfo, errorMessage, errorType) /// <summary> /// ErrorInfo 객체 설정하기 /// </summary> /// <param name="errorInfo">ErrorInfo 객체</param> /// <param name="errorMessage">에러 메시지</param> /// <param name="errorType">에러 타입</param> private void SetErrorInfo(ErrorInfo errorInfo, string errorMessage, ErrorType errorType) { errorInfo.ErrorText = errorMessage; errorInfo.ErrorType = errorType; } #endregion #region 빈 문자열 여부 조사하기 - IsStringEmpty(source) /// <summary> /// 빈 문자열 여부 조사하기 /// </summary> /// <param name="source">소스 문자열</param> /// <returns>빈 문자열 여부</returns> private bool IsStringEmpty(string source) { return (source != null && source.Trim().Length == 0); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" Width="800" Height="600" Title="IDXDataErrorInfo 인터페이스 : 데이터 검증하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <dxg:GridControl x:Name="gridControl"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="TaskName" /> <dxg:GridColumn FieldName="StartDate" /> <dxg:GridColumn FieldName="EndDate" /> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView ValidateRow="tableView_ValidateRow" InvalidRowException="tableView_InvalidRowException" /> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
using System; using System.Windows; using DevExpress.Xpf.Grid; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.gridControl.ItemsSource = TaskList.GetData(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 테이블 뷰 행 검증하기 - tableView_ValidateRow(sender, e) /// <summary> /// 테이블 뷰 행 검증하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void tableView_ValidateRow(object sender, GridRowValidationEventArgs e) { DateTime startDate = (e.Row as Task).StartDate; DateTime endDate = (e.Row as Task).EndDate; e.IsValid = startDate < endDate; } #endregion #region 테이블 뷰 적절치 못한 행 예외 처리하기 - tableView_InvalidRowException(sender, e) /// <summary> /// 테이블 뷰 적절치 못한 행 예외 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void tableView_InvalidRowException(object sender, InvalidRowExceptionEventArgs e) { e.ExceptionMode = ExceptionMode.NoAction; } #endregion } } |