■ ChartControl 클래스에서 ControlRowSource 객체를 사용해 GridControl 객체 데이터와 연동하는 방법을 보여준다.
▶ Revenue.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 |
using System; using System.Collections.Generic; namespace TestProject { /// <summary> /// 수입 /// </summary> public class Revenue { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 국가 배열 /// </summary> private static string[] _countrArray = new string[] { "한국", "미국", "중국", "러시아", "인도" }; /// <summary> /// 분야 배열 /// </summary> private static string[] _categoryArray = new string[] { "영화", "게임", "출판", "패션" }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 국가 - Country /// <summary> /// 국가 /// </summary> public string Country { get; set; } #endregion #region 분야 - Category /// <summary> /// 분야 /// </summary> public string Category { get; set; } #endregion #region 금액 - Amount /// <summary> /// 금액 /// </summary> public decimal Amount { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 리스트 구하기 - GetList(count) /// <summary> /// 리스트 구하기 /// </summary> /// <param name="count">카운트</param> /// <returns>리스트</returns> public static List<Revenue> GetList(int count) { Random random = new Random(DateTime.Now.Millisecond); List<Revenue> list = new List<Revenue>(); for(int i = 0; i < count; i++) { list.Add ( new Revenue { Country = _countrArray[random.Next(0, 4)], Category = _categoryArray[random.Next(0, 3)], Amount = random.Next(500, 5000) } ); } return list; } #endregion } } |
▶ MainForm.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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
using System.Windows.Forms; using DevExpress.Data.Controls; using DevExpress.Utils; using DevExpress.XtraCharts; using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Views.Grid; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); #region 그리드 뷰를 설정한다. InitializeView(this.gridView, true, false, true, false, false); AddColumn(this.gridView, "Country" , "국가", 100, HorzAlignment.Near, false, null , null ); AddColumn(this.gridView, "Category", "분야", 100, HorzAlignment.Near, false, null , null ); AddColumn(this.gridView, "Amount" , "금액", 100, HorzAlignment.Far , false, FormatType.Numeric, "#,##0"); this.gridControl.DataSource = Revenue.GetList(1000); this.gridView.BestFitColumns(); #endregion #region 차트 컨트롤을 설정한다. this.chartControl.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Right; this.chartControl.Legend.AlignmentVertical = LegendAlignmentVertical.Top; this.chartControl.SeriesTemplate.CrosshairLabelPattern = "{S}: {V:C2}"; this.chartControl.SeriesTemplate.SeriesPointsSorting = SortingMode.Ascending; this.chartControl.SeriesTemplate.SeriesPointsSortingKey = SeriesPointKey.Argument; this.chartControl.SeriesTemplate.SeriesDataMember = "Category"; this.chartControl.SeriesTemplate.ArgumentDataMember = "Country"; this.chartControl.SeriesTemplate.SummaryFunction = "SUM([Amount])"; chartControl.SeriesTemplate.ValueDataMembers.AddRange("Amount"); (this.chartControl.Diagram as XYDiagram).AxisY.Label.TextPattern = "{V:C0}"; this.chartControl.DataSource = new ControlRowSource() { Control = this.gridView, ControlRows = ControlRows.Selected }; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 뷰 초기화 하기 - InitializeView(gridView, multiSelect, allowFilter, allowSort, allowCellMerge, editable) /// <summary> /// 뷰 초기화 하기 /// </summary> /// <param name="gridView">그리드 뷰</param> /// <param name="multiSelect">멀티 선택 여부</param> /// <param name="allowFilter">필터 허용 여부</param> /// <param name="allowSort">정렬 허용 여부</param> /// <param name="allowCellMerge">셀 병합 사용 여부</param> /// <param name="editable">편집 허용 여부</param> private void InitializeView(GridView gridView, bool multiSelect, bool allowFilter, bool allowSort, bool allowCellMerge, bool editable) { gridView.OptionsDetail.EnableMasterViewMode = false; gridView.OptionsSelection.MultiSelect = multiSelect; gridView.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect; gridView.FocusRectStyle = DrawFocusRectStyle.None; // 그룹 패널 gridView.OptionsView.ShowGroupPanel = false; // 행 gridView.IndicatorWidth = 30; gridView.OptionsView.ShowIndicator = true; gridView.OptionsView.EnableAppearanceEvenRow = false; gridView.OptionsView.EnableAppearanceOddRow = false; gridView.OptionsBehavior.KeepFocusedRowOnUpdate = false; gridView.OptionsNavigation.AutoFocusNewRow = true; // 컬럼 gridView.OptionsBehavior.AutoPopulateColumns = false; gridView.OptionsView.ShowColumnHeaders = true; gridView.OptionsView.ColumnAutoWidth = false; gridView.OptionsMenu.EnableColumnMenu = false; gridView.OptionsNavigation.EnterMoveNextColumn = true; gridView.OptionsCustomization.AllowColumnMoving = false; gridView.OptionsCustomization.AllowColumnResizing = true; gridView.OptionsCustomization.AllowFilter = allowFilter; gridView.OptionsCustomization.AllowSort = allowSort; // 셀 gridView.OptionsView.ShowButtonMode = ShowButtonModeEnum.ShowForFocusedCell; gridView.OptionsView.AllowCellMerge = allowCellMerge; gridView.OptionsBehavior.EditorShowMode = EditorShowMode.Default; gridView.OptionsBehavior.Editable = editable; gridView.OptionsSelection.EnableAppearanceFocusedCell = editable; gridView.OptionsSelection.InvertSelection = false; gridView.OptionsHint.ShowCellHints = false; gridView.OptionsClipboard.CopyColumnHeaders = DefaultBoolean.True; } #endregion #region 컬럼 추가하기 - AddColumn(gridView, columnName, caption, columnWidth, hAlignment, allowEdit, displayFormatType, displayFormatString) /// <summary> /// 컬럼 추가하기 /// </summary> /// <param name="gridView">그리드 뷰</param> /// <param name="columnName">컬럼명</param> /// <param name="caption">제목</param> /// <param name="columnWidth">컬럼 너비</param> /// <param name="hAlignment">수평 정렬</param> /// <param name="allowEdit">편집 허용 여부</param> /// <param name="displayFormatType">디스플레이 포맷 타입</param> /// <param name="displayFormatString">디스플레이 포맷 문자열</param> /// <returns>그리드 컬럼</returns> private GridColumn AddColumn ( GridView gridView, string columnName, string caption, int columnWidth, HorzAlignment hAlignment, bool allowEdit, FormatType? displayFormatType, string displayFormatString ) { GridColumn column = gridView.Columns.AddVisible(columnName, caption); column.Name = columnName; column.Width = columnWidth; column.AppearanceHeader.Options.UseTextOptions = true; column.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center; column.AppearanceHeader.TextOptions.VAlignment = VertAlignment.Center; column.AppearanceCell.Options.UseTextOptions = true; column.AppearanceCell.TextOptions.HAlignment = hAlignment; column.AppearanceCell.TextOptions.VAlignment = VertAlignment.Center; column.OptionsColumn.AllowEdit = allowEdit; if(displayFormatType.HasValue) { column.DisplayFormat.FormatType = displayFormatType.Value; column.DisplayFormat.FormatString = displayFormatString; } return column; } #endregion } } |