차트의 Bar의 색상을 변경합니다.

 

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

using DevExpress.XtraCharts;

namespace Dev_Sample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            Load += mainForm_Load;
        }

        /// <summary>
        /// 메인폼 로드시 동작합니다.
        /// </summary>
        /// <param name="sender">이벤트 발생자 입니다.</param>
        /// <param name="e">이벤트 인자 입니다.</param>
        private void mainForm_Load(object sender, EventArgs e)
        {
            ChartControl chart = new ChartControl();

            chart.DataSource = CreateChartData();

            chart.SeriesDataMember = "Month";
            chart.SeriesTemplate.ArgumentDataMember = "Section";
            chart.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "Value" });

            chart.SeriesTemplate.View = new SideBySideBarSeriesView();

            chart.SeriesNameTemplate.BeginText = "Month : ";

            chart.SeriesNameTemplate.BeginText = "Month : ";

            chart.BoundDataChanged += chart_BoundDataChanged;

            chart.Dock = DockStyle.Fill;
            this.Controls.Add(chart);
        }

        private void chart_BoundDataChanged(object sender, EventArgs e)
        {
            ChartControl chart = (ChartControl)sender;

            for (int i = 0; i < chart.Series.Count; i++)
            {
                BarSeriesView view = (BarSeriesView)chart.Series[i].View;
                view.Color = Color.Blue;
                view.FillStyle.FillMode = FillMode.Gradient;
                ((GradientFillOptionsBase)view.FillStyle.Options).Color2 = Color.Beige;
            }
        }

        private DataTable CreateChartData()
        { 
            DataTable table = new DataTable("Table");

            table.Columns.Add("Month", typeof(string));
            table.Columns.Add("Section", typeof(string));
            table.Columns.Add("Value", typeof(Int32));

            table.Rows.Add(new object[] { "Jan"  , "Section1", 10 });
            table.Rows.Add(new object[] { "Jan"  , "Section2", 20 });
            table.Rows.Add(new object[] { "Jan"  , "Section3", 40 });
            table.Rows.Add(new object[] { "Feb"  , "Section1", 20 });
            table.Rows.Add(new object[] { "Feb"  , "Section2", 30 });
            table.Rows.Add(new object[] { "Feb"  , "Section3", 80 });
            table.Rows.Add(new object[] { "March", "Section1", 30 });
            table.Rows.Add(new object[] { "March", "Section2", 40 });
            table.Rows.Add(new object[] { "March", "Section3", 100 });

            return table;
        }
    }
}

 

차트에 spline, line, bar를 출력합니다.

using System;
using System.Data;
using System.Windows.Forms;

using DevExpress.XtraCharts;

namespace Dev_Sample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            Load += mainForm_Load;
        }

        /// <summary>
        /// 메인폼 로드시 동작합니다.
        /// </summary>
        /// <param name="sender">이벤트 발생자 입니다.</param>
        /// <param name="e">이벤트 인자 입니다.</param>
        private void mainForm_Load(object sender, EventArgs e)
        {
            ChartControl chart = new ChartControl();

            chart.DataSource = CreateChartData();

            chart.SeriesDataMember = "Month";
            chart.SeriesTemplate.ArgumentDataMember = "Section";
            chart.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "Value" });

            chart.SeriesTemplate.View = new SideBySideBarSeriesView();

            chart.SeriesNameTemplate.BeginText = "Month : ";

            chart.SeriesNameTemplate.BeginText = "Month : ";

            chart.BoundDataChanged += chart_BoundDataChanged;

            chart.Dock = DockStyle.Fill;
            this.Controls.Add(chart);
        }

        private void chart_BoundDataChanged(object sender, EventArgs e)
        {
            ChartControl chart = (ChartControl)sender;

            Series series = chart.GetSeriesByName("Month : Feb");

            if(series != null)
                series.ChangeView(ViewType.Line);

            Series march = chart.GetSeriesByName("Month : March");

            if(march != null)
                march.ChangeView(ViewType.Spline);
        }

        private DataTable CreateChartData()
        { 
            DataTable table = new DataTable("Table");

            table.Columns.Add("Month", typeof(string));
            table.Columns.Add("Section", typeof(string));
            table.Columns.Add("Value", typeof(Int32));

            table.Rows.Add(new object[] { "Jan"  , "Section1", 10 });
            table.Rows.Add(new object[] { "Jan"  , "Section2", 20 });
            table.Rows.Add(new object[] { "Jan"  , "Section3", 40 });
            table.Rows.Add(new object[] { "Feb"  , "Section1", 20 });
            table.Rows.Add(new object[] { "Feb"  , "Section2", 30 });
            table.Rows.Add(new object[] { "Feb"  , "Section3", 80 });
            table.Rows.Add(new object[] { "March", "Section1", 30 });
            table.Rows.Add(new object[] { "March", "Section2", 40 });
            table.Rows.Add(new object[] { "March", "Section3", 100 });

            return table;
        }
    }
}

 

Bar 차트를 생성합니다

 

using System;
using System.Data;
using System.Windows.Forms;

using DevExpress.XtraCharts;

namespace Dev_Sample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            Load += mainForm_Load;
        }

        /// <summary>
        /// 메인폼 로드시 동작합니다.
        /// </summary>
        /// <param name="sender">이벤트 발생자 입니다.</param>
        /// <param name="e">이벤트 인자 입니다.</param>
        private void mainForm_Load(object sender, EventArgs e)
        {
            ChartControl chart = new ChartControl();

            chart.DataSource = CreateChartData();

            chart.SeriesDataMember = "Month";
            chart.SeriesTemplate.ArgumentDataMember = "Section";
            chart.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "Value" });

            chart.SeriesTemplate.View = new StackedBarSeriesView();

            chart.SeriesNameTemplate.BeginText = "Month : ";

            chart.Dock = DockStyle.Fill;
            this.Controls.Add(chart);
        }

        private DataTable CreateChartData()
        { 
            DataTable table = new DataTable("Table");

            table.Columns.Add("Month", typeof(string));
            table.Columns.Add("Section", typeof(string));
            table.Columns.Add("Value", typeof(Int32));

            table.Rows.Add(new object[] { "Jan", "Section1", 10 });
            table.Rows.Add(new object[] { "Jan", "Section2", 20 });
            table.Rows.Add(new object[] { "Jan", "Section1", 20 });
            table.Rows.Add(new object[] { "Jan", "Section2", 30 });
            table.Rows.Add(new object[] { "Jan", "Section1", 15 });
            table.Rows.Add(new object[] { "Jan", "Section2", 25 });

            return table;
        }
    }
}

개별 차트 시리즈를 데이터에 바인딩하는 예제 입니다.

 

using System;
using System.Data;
using System.Windows.Forms;

using DevExpress.XtraCharts;

namespace Dev_Sample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            Load += mainForm_Load;
        }

        /// <summary>
        /// 메인폼 로드시 동작합니다.
        /// </summary>
        /// <param name="sender">이벤트 발생자 입니다.</param>
        /// <param name="e">이벤트 인자 입니다.</param>
        private void mainForm_Load(object sender, EventArgs e)
        {
            ChartControl chart = new ChartControl();

            Series series = new Series("Series1", ViewType.Bar);
            chart.Series.Add(series);

            series.DataSource = CreateChartData(50);

            series.ArgumentScaleType = ScaleType.Numerical;
            series.ArgumentDataMember = "Argument";
            series.ValueScaleType = ScaleType.Numerical;
            series.ValueDataMembers.AddRange(new string[] { "Value" });

            ((SideBySideBarSeriesView)series.View).ColorEach = true;
            ((XYDiagram)chart.Diagram).AxisX.Visibility = DevExpress.Utils.DefaultBoolean.False;
            chart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;

            chart.Dock = DockStyle.Fill;
            this.Controls.Add(chart);
        }

        private DataTable CreateChartData(int rowCount)
        { 
            DataTable table = new DataTable("Table");

            table.Columns.Add("Argument", typeof(Int32));
            table.Columns.Add("Value", typeof(Int32));

            Random random = new Random();
            DataRow row = null;

            for (int i = 0; i < rowCount; i++)
            {
                row = table.NewRow();
                row["Argument"] = i;
                row["Value"] = random.Next(100);
                table.Rows.Add(row);
            }

            return table;
        }
    }
}

 

Unbound Column 생성하기 입니다.

GridColumn unboundColumn = this.gridView.Columns.AddField("Test");
unboundColumn.VisibleIndex = this.gridView.Columns.Count;
unboundColumn.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
unboundColumn.OptionsColumn.AllowEdit = false;

위의 코드를 작성하여 Test라는 이름을 가진 언 바운드 컬럼을 생성합니다.

 

private void gridView_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
	GridView view = sender as GridView;

	if(e.Column.FieldName == "Test" && e.IsGetData)
	{ 
		e.Value = "Test Data";
	}
}

위의 코드를 작성하여 해당 컬럼에 값을 넣어줍니다.

특정 행의 색을 변경합니다.

 

private void gridView_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
	var row = this.gridView.GetRowCellDisplayText(e.RowHandle, "ID");

	if(row.Equals("aaa"))
	{ 
		e.Appearance.BackColor = Color.DarkOrange;
	}
}

GetRowCellDisplayText 메서드에서 두번째 인자는 컬럼 이름을 작성하면 됩니다.

TextEdit에서 천 단위로 끊어서 표기하기(ex: 2,300) 입니다.

 

 this.textEdit.Properties.Mask.EditMask = "n0";
 this.textEdit.Properties.Mask.UseMaskAsDisplayFormat = true;
 this.textEdit.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;

위의 코드를 생성자 안에 작성하면 됩니다.

DevExpress에서 DxValidationProvider 를 사용하여 유효성 검사를 합니다.

 

using System.Windows.Forms;

using DevExpress.XtraEditors.DXErrorProvider;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            #region 유효성 검사 입니다.

            this.dxValidationProvider1.ValidationMode = DevExpress.XtraEditors.DXErrorProvider.ValidationMode.Manual;

            ConditionValidationRule validationRule = new ConditionValidationRule();

            validationRule.ConditionOperator = ConditionOperator.IsNotBlank;
            validationRule.Value1 = "";

            validationRule.ErrorText = "Please enter a value that equals to the first editor's value";
            validationRule.ErrorType = ErrorType.Critical;

            dxValidationProvider1.SetValidationRule(this.textEdit1, validationRule);

            #endregion

            #region 이벤트를 설정합니다.

            this.simpleButton1.Click += SimpleButton1_Click;

            #endregion
        }

        /// <summary>
        /// 버튼 클릭시 동작합니다.
        /// </summary>
        /// <param name="sender">이벤트 발생자 입니다.</param>
        /// <param name="e">이벤트 인자 입니다.</param>
        private void SimpleButton1_Click(object sender, System.EventArgs e)
        {
            dxValidationProvider1.Validate();
        }
    }
}

DevExpress를 사용하여 로컬 드라이브에서 TreeList를 사용하여 디렉토리 구조를 확인할 수 있습니다.

 

예제 결과 입니다.

using System.IO;
using System.Windows.Forms;

using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;

namespace DevTestForm
{
    /// <summary>
    /// 메인폼 클래스 입니다.
    /// </summary>
    public partial class MainForm : Form
    {   
        // Constructor (Public)

        #region MainForm() - 생성자 입니다.

        /// <summary>
        /// 생성자 입니다.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            InitTreeList();
            InitData();

            #region 이벤트를 설정합니다.

            #endregion
        }

        #endregion

        private void InitTreeList()
        {
            TreeListColumn fullNameColumn = new TreeListColumn();
            TreeListColumn nameColumn     = new TreeListColumn();
            TreeListColumn typeColumn     = new TreeListColumn();
            TreeListColumn sizeColumn     = new TreeListColumn();

            fullNameColumn.Caption   = "FullName";
            fullNameColumn.FieldName = "FullName";

            nameColumn.Caption      = "Name";
            nameColumn.FieldName    = "Name";
            nameColumn.MinWidth     = 27;
            nameColumn.VisibleIndex = 0;
            nameColumn.Width        = 274;

            typeColumn.Caption      = "Type";
            typeColumn.FieldName    = "Type";
            typeColumn.VisibleIndex = 1;
            typeColumn.Width        = 112;

            sizeColumn.AppearanceCell.Options.UseTextOptions = true;
            sizeColumn.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            sizeColumn.Caption                               = "Size(Bytes)";
            sizeColumn.FieldName                             = "Size";
            sizeColumn.Format.FormatType                     = DevExpress.Utils.FormatType.Numeric;
            sizeColumn.Format.FormatString                   = "n0";
            sizeColumn.VisibleIndex                          = 2;
            sizeColumn.Width                                 = 123;

            this.devTreeList.Columns.AddRange(new TreeListColumn[]
            {
                fullNameColumn, nameColumn, typeColumn, sizeColumn
            });

            this.devTreeList.Dock                                   = DockStyle.Fill;
            this.devTreeList.OptionsBehavior.AutoChangeParent       = false;
            this.devTreeList.OptionsBehavior.AutoNodeHeight         = false;
            this.devTreeList.OptionsBehavior.CloseEditorOnLostFocus = false;
            this.devTreeList.OptionsBehavior.Editable               = false;
            this.devTreeList.OptionsBehavior.ShowToolTips           = false;
            this.devTreeList.OptionsBehavior.SmartMouseHover        = false;
            this.devTreeList.OptionsSelection.KeepSelectedOnClick   = false;

            this.devTreeList.StateImageList = this.imageList;

            #region 이벤트를 설정합니다.

            this.devTreeList.AfterCollapse += devTreeList_AfterCollapse;
            this.devTreeList.AfterExpand   += devTreeList_AfterExpand;
            this.devTreeList.BeforeExpand  += devTreeList_BeforeExpand;

            #endregion

        }

        // Event Method (Private)

        #region devTreeList_AfterCollapse(sender, e)

        /// <summary>
        /// 트리리스트의 노드가 접힌 후에 동작합니다.
        /// </summary>
        /// <param name="sender">이벤트 발생자 입니다.</param>
        /// <param name="e">이벤트 인자 입니다.</param>
        private void devTreeList_AfterCollapse(object sender, NodeEventArgs e)
        {
            if(e.Node.StateImageIndex != 1)
            { 
                e.Node.StateImageIndex = 0;
            }
        }

        #endregion

        #region devTreeList_AfterExpand(sender, e)

        /// <summary>
        /// 트리리스트의 노드가 펼쳐진 후에 동작합니다.
        /// </summary>
        /// <param name="sender">이벤트 발생자 입니다.</param>
        /// <param name="e">이벤트 인자 입니다.</param>
        private void devTreeList_AfterExpand(object sender, NodeEventArgs e)
        {
            if (e.Node.StateImageIndex != 1)
            {
                e.Node.StateImageIndex = 2;
            }
        }

        #endregion

        #region devTreeList_BeforeExpand(sender, e)

        /// <summary>
        /// 트리리스트의 노드가 펼쳐지기 전에 동작합니다.
        /// </summary>
        /// <param name="sender">이벤트 발생자 입니다.</param>
        /// <param name="e">이벤트 인자 입니다.</param>
        private void devTreeList_BeforeExpand(object sender, BeforeExpandEventArgs e)
        {
            if(e.Node.Tag != null)
            { 
                Cursor currentCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
                InitFolders(e.Node.GetDisplayText("FullName"), e.Node);
                e.Node.Tag = null;
                Cursor.Current = currentCursor;
            }
        }

        #endregion

        #region InitData() - 데이터를 초기화합니다.

        /// <summary>
        /// 데이터를 초기화합니다.
        /// </summary>
        private void InitData()
        { 
            InitFolders(Directory.GetDirectoryRoot(Directory.GetCurrentDirectory()), null);
        }

        #endregion

        #region InitFolders(path, node) - 폴더를 초기화 합니다.

        /// <summary>
        /// 폴더를 초기화 합니다.
        /// </summary>
        /// <param name="path">경로 입니다.</param>
        /// <param name="node">노드 입니다.</param>
        private void InitFolders(string path, TreeListNode node)
        {
            this.devTreeList.BeginUnboundLoad();
            
            TreeListNode treeListNode;
            DirectoryInfo directoryInfo;

            try
            {
                string[] root = Directory.GetDirectories(path);

                foreach(string item in root)
                {
                    try
                    {
                        directoryInfo = new DirectoryInfo(item);
                        treeListNode                 = this.devTreeList.AppendNode
                        (
                            new object[] { item, directoryInfo.Name, "Folder", null }, node
                        );
                        treeListNode.StateImageIndex = 0;
                        treeListNode.HasChildren     = HasFiles(item);

                        if (treeListNode.HasChildren)
                        {
                            treeListNode.Tag = true;
                        }
                    }
                    catch{ }
                    
                }
            }
            catch{ }

            InitFiles(path, node);
            this.devTreeList.EndUnboundLoad();
        }

        #endregion

        #region InitFiles(path, node) - 파일을 초기화 합니다.

        /// <summary>
        /// 파일을 초기화 합니다.
        /// </summary>
        /// <param name="path">경로 입니다.</param>
        /// <param name="node">노드 입니다.</param>
        private void InitFiles(string path, TreeListNode node)
        {
            TreeListNode treeListNode;

            FileInfo fileInfo;

            try
            {
                string[] root = Directory.GetFiles(path);

                foreach(string item in root)
                {
                    fileInfo = new FileInfo(item);

                    treeListNode                 = this.devTreeList.AppendNode
                    (
                        new object[] { item, fileInfo.Name, "File", fileInfo.Length }, node
                    );

                    treeListNode.StateImageIndex = 1;
                    treeListNode.HasChildren     = false;
                }
            }
            catch { }
        }

        #endregion

        #region HasFiles(path) - 파일의 존재 여부를 확인합니다.

        /// <summary>
        /// 파일의 존재 여부를 확인합니다.
        /// </summary>
        /// <param name="path">경로 입니다.</param>
        /// <returns>파일 유무를 리턴합니다.</returns>
        private bool HasFiles(string path)
        {
            string[] root = Directory.GetFiles(path);

            if(root.Length > 0)
            { 
                return true;
            }

            root = Directory.GetDirectories(path);

            if(root.Length > 0)
            { 
                return true;
            }

            return false;
        }

        #endregion
    }
}

DevExpress에서 TreeList에서 노드 추가하기 입니다.

 

TreeList에서 노드 추가한 결과 입니다.

using System.Windows.Forms;

using DevExpress.XtraEditors;
using DevExpress.XtraLayout;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;

namespace DevTestForm
{
    /// <summary>
    /// 메인폼 클래스 입니다.
    /// </summary>
    public partial class MainForm : Form
    {   
        // Constructor (Public)

        #region MainForm() - 생성자 입니다.

        /// <summary>
        /// 생성자 입니다.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            CreateColumns(this.devTreeList);
            CreateNodes(this.devTreeList);

            #region 이벤트를 설정합니다.

            #endregion
        }

        #endregion


        #region CreateColumns(treeList) - 컬럼을 생성합니다.

        /// <summary>
        /// 컬럼을 생성합니다.
        /// </summary>
        /// <param name="treeList">TreeList 입니다.</param>
        private void CreateColumns(TreeList treeList)
        { 
            treeList.BeginUpdate();

            TreeListColumn column1 = treeList.Columns.Add();
            column1.Caption      = "Customer";
            column1.VisibleIndex = 0;

            TreeListColumn column2 = treeList.Columns.Add();
            column2.Caption      = "Location";
            column2.VisibleIndex = 1;

            TreeListColumn column3 = treeList.Columns.Add();
            column3.Caption      = "Phone";
            column3.VisibleIndex = 2;

            treeList.EndUpdate();
        }

        #endregion

        #region CreateNodes(treeList)

        /// <summary>
        /// 노드를 생성합니다.
        /// </summary>
        /// <param name="treeList">TreeList 입니다.</param>
        private void CreateNodes(TreeList treeList)
        { 
            treeList.BeginUnboundLoad();

            TreeListNode parentForRootNodes = null;
            TreeListNode rootNode = treeList.AppendNode
            (
                new object[]{"AAAAAA", "BBBBBB", "000-0000-0000"}, parentForRootNodes
            );

            treeList.AppendNode
            (
                new object[] { "CCCCCC", "DDDDDDD", "1111-1111-1111" }, rootNode
            );

            treeList.AppendNode
            (
                new object[] { "ABB", "ADDADA", "222-2222-1111" }, rootNode
            );

            treeList.EndUnboundLoad();
        }

        #endregion

    }
}

+ Recent posts