[C# DevExpress] 윈폼 DevExpress GridControl Cell Merge(셀 병합) 하기


안녕하세요.

 

오늘은 윈폼 DevExpress GridControl 에서 Cell Merge(셀 병합) 하는 방법에 대해서 알려 드리려고 합니다.

 

그럼 바로 예제 코드를 통해서 알아보도록 할게요.

 

먼저, 빈 윈폼 프로젝트를 생성해 주시고 아래와 같이 GirdControl을 배치해 주시기 바랍니다.


 

윈폼 프로젝트 생성 및 GridControl 컨트롤 배치




위와 같이 컨트롤을 배치해 주시기 바랍니다.

 

그럼 이제 예제 코드를 통해서 Cell Merge를 어떻게 하는지 알아 보도록 할게요.

 

먼저, 소스코드를 작성하기 전에 GridView 속성에서 OptionView가 있는데 여기서 AllowCellMergeFalse -> True 로 변경해 주셔야 합니다.

 


 

위와 같이 설정을 변경하셨다면 예제 코드를 작성해 주시기 바랍니다.


예제 코드


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

using DevExpress.Spreadsheet;

using DevExpress.Utils;

using DevExpress.XtraCharts;

using DevExpress.XtraEditors.Repository;

using DevExpress.XtraGrid.Views.Grid;

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace DevTest

{

    public partial class Form1 : DevExpress.XtraEditors.XtraForm

    {

        DataTable dt = null;

 

        public Form1()

        {

            InitializeComponent();

 

            // Shown 이벤트 선언

            this.Shown += DevForm_Shown;

        }

 

        /// <summary>

        /// 폼이 보여질 일어나는 이벤트 핸들러

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        public void DevForm_Shown(object sender, EventArgs e)

        {

            //그리드 설정  데이터 넣기

            InitGrid();

        }

 

        public void InitGrid()

        {

            uiView_Main.OptionsView.ShowGroupPanel = false;

            uiView_Main.OptionsBehavior.Editable = false;

 

            this.uiView_Main.Columns.Clear();

 

            dt = GetData();

            uiGrid_Main.DataSource = dt;

 

            //Cell Merge 이벤트 선언

            uiView_Main.CellMerge += CellMerge_Event;

 

            //Column, Row 폰트 사이즈종류 변경

            uiView_Main.Appearance.HeaderPanel.Font = new Font("Arial"9, FontStyle.Bold);

            uiView_Main.Appearance.Row.Font = new Font("Arial"9, FontStyle.Bold);

 

            ////컬럼 Header 가운데 정렬

            for (int idx = 0; idx < dt.Columns.Count; idx++)

            {

                uiView_Main.Columns[idx].AppearanceHeader.TextOptions.HAlignment =

                    DevExpress.Utils.HorzAlignment.Center;

            }

        }

 

        /// <summary>

        /// 테스트 데이터 선언

        /// </summary>

        /// <returns></returns>

        public DataTable GetData()

        {

            dt = new DataTable();

 

            dt.Columns.Add("Name");

            dt.Columns.Add("Age");

            dt.Columns.Add("Sex");

            dt.Columns.Add("Grade");

 

            dt.Rows.Add(new string[] { "범범조조""28""남성""A"});

            dt.Rows.Add(new string[] { "백예린""25""여성""B+" });

            dt.Rows.Add(new string[] { "아이유""28""여성""A" });

            dt.Rows.Add(new string[] { "강다니엘""24""남성""A+" });

            dt.Rows.Add(new string[] { "안정환""46""남성""B" });

            dt.Rows.Add(new string[] { "김성주""47""남성""A" });

 

            return dt;

        }

        

        /// <summary>

        ///  병합 이벤트 핸들러

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        void CellMerge_Event(object sender, 

DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e)

        {

            if (e.Column.FieldName == "Sex")//설병 컬럼만 Merge

            {

                var dr1 = uiView_Main.GetDataRow(e.RowHandle1); //위에  정보

                var dr2 = uiView_Main.GetDataRow(e.RowHandle2); //아래  정보

 

                //비교하는 이유 그래야 정상적으로 나옴.

                e.Merge = dr1["Sex"].ToString().Equals(dr2["Sex"].ToString());

            }

            else

                e.Merge = false;

 

            e.Handled = true;

        }

    }

}

 

Colored by Color Scripter

cs


실행 결과




위와 같이 성별컬럼에 남성, 여성 끼리 CellMerge된 것을 확인하실 수 있습니다.

 

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY