첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ CustomObjectConverter.cs

using System;

using DevExpress.Utils.Serializing.Helpers;

namespace TestProject
{
    /// <summary>
    /// 커스텀 객체 컨버터
    /// </summary>
    public class CustomObjectConverter : ICustomObjectConverter
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 변환 가능 여부 구하기 - CanConvert(type)

        /// <summary>
        /// 변환 가능 여부 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <returns>변환 가능 여부</returns>
        public bool CanConvert(Type type)
        {
            return type == typeof(Employee);
        }

        #endregion
        #region 문자열에서 객체 구하기 - FromString(type, source)

        /// <summary>
        /// 문자열에서 객체 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <param name="source">소스</param>
        /// <returns>객체</returns>
        public object FromString(Type type, string source)
        {
            if(type != typeof(Employee))
            {
                return null;
            }

            string[] itemArray = source.Split('#');

            if(itemArray.Length >= 3)
            {
                return new Employee(itemArray[0], itemArray[1], int.Parse(itemArray[2]));
            }
            else if(itemArray.Length == 2)
            {
                return new Employee(itemArray[0], itemArray[1], 0);
            }
            else if(itemArray.Length == 1)
            {
                return new Employee(itemArray[0], string.Empty, 0);
            }
            else
            {
                return new Employee(string.Empty, string.Empty, 0);
            }
        }

        #endregion
        #region 문자열 구하기 - ToString(type, source)

        /// <summary>
        /// 문자열 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <param name="source">소스</param>
        /// <returns>문자열</returns>
        public string ToString(Type type, object source)
        {
            if(type != typeof(Employee))
            {
                return string.Empty;
            }

            Employee employee = source as Employee;

            return employee.FirstName + '#' + employee.LastName + '#' + employee.Age;
        }

        #endregion
        #region 타입 구하기 - GetType(typeName)

        /// <summary>
        /// 타입 구하기
        /// </summary>
        /// <param name="typeName">타입명</param>
        /// <returns>타입</returns>
        public Type GetType(string typeName)
        {
            if(typeName != typeof(Employee).FullName)
            {
                return null;
            }

            return typeof(Employee);
        }

        #endregion
    }
}

 

728x90

 

▶ MainForm.cs

using System;
using System.IO;

using DevExpress.XtraEditors;
using DevExpress.Data.PivotGrid;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : XtraForm
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 스트림
        /// </summary>
        private MemoryStream stream;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.pivotGridControl.DataSource = Employee.GetDataTable();

            this.pivotGridControl.OptionsData.CustomObjectConverter = new CustomObjectConverter();

            this.saveButton.Click += saveButton_Click;
            this.loadButton.Click += loadButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Save 버튼 클릭시 처리하기 - saveButton_Click(sender, e)

        /// <summary>
        /// Save 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void saveButton_Click(object sender, EventArgs e)
        {
            if(this.stream != null)
            {
                this.stream.Dispose();
            }

            this.stream = new MemoryStream();

            this.pivotGridControl.SavePivotGridToStream(stream);
        }

        #endregion
        #region Load 버튼 클릭시 처리하기 - loadButton_Click(sender, e)

        /// <summary>
        /// Load 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void loadButton_Click(object sender, EventArgs e)
        {
            if(this.stream == null)
            {
                return;
            }

            PivotFileDataSource dataSource = new PivotFileDataSource(this.stream, new CustomObjectConverter());

            this.pivotGridControl.DataSource = dataSource;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요