■ GridControl 클래스에서 커스텀 그룹을 구현하는 방법을 보여준다.
▶ 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 27 28 29 30 31 32 33 34 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" Width="800" Height="600" Title="커스텀 그룹 구현하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <dxg:GridControl x:Name="gridControl" CustomColumnGroup="gridControl_CustomColumnGroup" CustomGroupDisplayText="gridControl_CustomGroupDisplayText"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="ProductName" /> <dxg:GridColumn FieldName="UnitsInStock" /> <dxg:GridColumn FieldName="UnitsOnOrder" /> <dxg:GridColumn FieldName="UnitPrice" SortMode="Custom"> <dxg:GridColumn.EditSettings> <dxe:SpinEditSettings DisplayFormat="c2" /> </dxg:GridColumn.EditSettings> </dxg:GridColumn> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView /> </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 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 |
using System; using System.Collections; 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 = new NorthwindEntities().Products; this.gridControl.Columns["UnitPrice"].GroupIndex = 0; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 그리드 컨트롤 커스텀 컬럼 그룹 처리하기 - gridControl_CustomColumnGroup(sender, e) /// <summary> /// 그리드 컨트롤 커스텀 컬럼 그룹 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridControl_CustomColumnGroup(object sender, CustomColumnSortEventArgs e) { if(e.Column.FieldName == "UnitPrice") { double value1 = Math.Floor(Convert.ToDouble(e.Value1) / 10); double value2 = Math.Floor(Convert.ToDouble(e.Value2) / 10); int compareResult = Comparer.Default.Compare(value1, value2); if(value1 > 9 && value2 > 9) { compareResult = 0; } e.Result = compareResult; e.Handled = true; } } #endregion #region 그리드 컨트롤 커스텀 그룹 표시 텍스트 처리하기 - gridControl_CustomGroupDisplayText(sender, e) /// <summary> /// 그리드 컨트롤 커스텀 그룹 표시 텍스트 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridControl_CustomGroupDisplayText(object sender, CustomGroupDisplayTextEventArgs e) { if(e.Column.FieldName != "UnitPrice") { return; } int groupLevel = this.gridControl.GetRowLevelByRowHandle(e.RowHandle); if(groupLevel != e.Column.GroupIndex) { return; } string interval = GetIntervalString(e.Value); e.DisplayText = interval; } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 간격 문자열 구하기 - GetIntervalString(value) /// <summary> /// 간격 문자열 구하기 /// </summary> /// <param name="value">값</param> /// <returns>간격 문자열</returns> private string GetIntervalString(object value) { double valueDouble = Math.Floor(Convert.ToDouble(value) / 10); string interval = string.Format("{0:c} - {1:c} ", valueDouble * 10, (valueDouble + 1) * 10); if(valueDouble > 9) { interval = string.Format(">= {0:c} ", 100); } return interval; } #endregion } } |