[C# DevExpress] C# DevExpress GridControl에서 Row 데이터 가운데 정렬하기


안녕하세요.

 

오늘은 C# DevExpress에서 제공하는 GridControl 중에 Row 데이터 값들을 가운데 정렬 하는 방법에 대해서 알려 드리려고 합니다.

 

바로 예제를 통해 보여 드리도록 할게요.


 

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


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



 

그럼 이제 Row 데이터를 가운데 정렬하는 예제 코드를 작성해 보도록 하겠습니다.


예제 코드


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

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;

 

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

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

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

 

            //Cell 이벤트 선언

            uiView_Main.RowCellStyle += UiView_Main_RowCellStyle;

        }

 

        /// <summary>

        /// RowCellStyle 이벤트

        /// Row 데이터 정렬

        /// </summary>

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

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

        private void UiView_Main_RowCellStyle(object sender, 

DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)

        {

            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            e.Appearance.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;

        }

 

 

        /// <summary>

        /// 테스트 데이터 선언

        /// </summary>

        /// <returns></returns>

        public DataTable GetData()

        {

            dt = new DataTable();

 

            dt.Columns.Add("Name");

            dt.Columns.Add("Age");

            dt.Columns.Add("Grade");

 

            dt.Rows.Add("범범조조""28""3");

            dt.Rows.Add("헤이즈""25""4");

            dt.Rows.Add("아이유""28""2");

            dt.Rows.Add("권정렬""34""1");

            dt.Rows.Add("유세윤""38""2");

 

            return dt;

        }

    }

}

 

Colored by Color Scripter

cs


실행 결과



예제 코드는 꽤 길어 보이지만..가운데 정렬하는 핵심은 RowCellStyle 이벤트 핸들러 입니다.

 

RowCellStyle 이벤트 안에 보시면 e.Appearance.TextOptions.HAlignment , e.Appearance.TextOptions.VAlignment 의 속성을 Center로 설정해 주시면 위와 같이 GridRow 데이터들이 가운데 정렬 되어 보여지는 것을 확인하실 수 있습니다.

 

감사합니다.


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY