[C# DevExpress] 윈폼 DevExpress GridControl(그리드컨트롤) Column 데이터 넣기, 가져오기


안녕하세요.

 

오늘은 윈폼 DevExpress 에서 기본으로 제공해주는 컨트롤인 GridControl에 대해서 알아보려고 합니다.

 

그 중에서도, 그리드컨트롤 특정 컬럼에 데이터를 넣고, 가져오는 방법을 알아 보려고 해요.

 

그럼 바로 예제 프로그램을 만들어 보도록 하겠습니다.


 

윈폼 프로젝트 생성 및 DataControl, SimpleButton 배치하기



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


 

그럼 이제 CellClick을 했을 때, 해당 Row의 데이터 값들을 MessageBox로 보여주고,

 

Save 버튼을 클릭했을 시, 기존의 데이터의 다른 데이터를 넣어 보도록 그렇게 예제 코드를 작성해 보도록 하겠습니다.

 

예제 코드


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

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;

 

            //버튼 클릭 이벤트 선언

            this.uiBtn_Save.Click += uiBtn_Save_Click;

        }

 

        /// <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 클릭 이벤트 선언

            uiView_Main.RowClick += RowClick_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;

            }

        }

 

        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>

        /// Save 버튼 클릭 이벤트 핸들러

        /// </summary>

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

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

        public void uiBtn_Save_Click(object sender, EventArgs e)

        {

            for (int i = 0; i < uiView_Main.RowCount; i++)

            {

                //인덱스컬럼 이름넣은 데이터 

                uiView_Main.SetRowCellValue(i, "Grade"" 넣기"); 

            }

        }

 

        /// <summary>

        /// Row 클릭 이벤트 핸들러

        /// </summary>

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

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

        private void RowClick_Event(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)

        {

            if (e.Button != MouseButtons.Right) return;

 

            string name = string.Empty;

            string age = string.Empty;

            string sex = string.Empty;

            string grade = string.Empty;

 

            name = Convert.ToString(uiView_Main.GetRowCellValue(e.RowHandle, "Name"));

            age = Convert.ToString(uiView_Main.GetRowCellValue(e.RowHandle, "Age"));

            sex = Convert.ToString(uiView_Main.GetRowCellValue(e.RowHandle, "Sex"));

            grade = Convert.ToString(uiView_Main.GetRowCellValue(e.RowHandle, "Grade"));

 

            string msg = 

                string.Format("이름 : {0}, 나이 : {1}, 성별 : {2}, 성적 : {3}", name, age, sex, grade);

 

            MessageBox.Show(msg);

        }

    }

}

 

Colored by Color Scripter

cs


기본 결과 화면




Row 클릭 결과




Save 버튼 클릭 결과



위와 같이 특정 Row 를 클릭했을 시, 해당 Row의 데이터를 가져와 메시지박스를 띄어주는 것을 확인하실 수 있습니다.

 

그리고 제가 코드에서 Save 버튼을 클릭하면 Grade 컬럼의 데이터를 값 넣기로 바꿔서 다시 넣게 끔 소스코드를 작성했는데, 이 또한 제대로 동작한 것을 확인하실 수 있습니다.

 

이로써, GridControl에서 데이터를 가져오고, 넣는 방법에 대해서 알아 보았습니다.

 

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY