[C# DevExpress] 윈폼 DevExpress(데브익스프레스) GridControl 데이터 넣기


안녕하세요.

 

오늘은 C# 윈폼에서 DevExpress(데브익스프레스)에서 Grid 컨트롤을 기본적으로 다뤄보도록 하려고 합니다.

 

Grid컨트롤은 흔히 C# 윈폼에서 기본으로 제공해주는 DataGridView 컨트롤이라고 생각하시면 되겠습니다.

 

오늘은 간단히 DataTable에 데이터를 넣어서 GridControl에 데이터 바인딩 하여 데이터 넣는 방법에 대해서 알려드리려고 합니다.


 

소스코드는 매우 간단하기 때문에, 별도의 설명 없이 예제 코드와 함께 바로 보여드리도록 하겠습니다.

 

먼저 빈 윈폼 프로젝트를 생성해 주시고, 그 안에다가 GridControl을 배치 주시기 바랍니다.


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



 

그럼 이제 소스코드로 넘어와서, 임의의 학생 DataTable 을 만들어서 그 데이터를 GridControl에 바인딩 해 보도록 하겠습니다.


예제 코드


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

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

    {

        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)

        {

            //GridControl 초기화

            InitGridControl();

 

            //DataTable 데이터 저장  바인딩

            this.uiGrid_Main.DataSource = GetData();

        }

 

        /// <summary>

        /// GridView 초기화

        /// </summary>

        public void InitGridControl()

        {

            GridView gv = this.uiGrid_Main.MainView as GridView;

            gv.OptionsView.ShowGroupPanel = false;

            gv.OptionsBehavior.Editable = false;

        }

 

        public DataTable GetData()

        {

            DataTable dt = new DataTable();

            dt.Columns.Add("Name");

            dt.Columns.Add("Age");

            dt.Columns.Add("Grade");

            dt.Columns.Add("Score");

 

            dt.Rows.Add(new string[] { "범범조조""28""2""100" });

            dt.Rows.Add(new string[] { "황선홍""56""3""10" });

            dt.Rows.Add(new string[] { "안정환""34""2""78" });

            dt.Rows.Add(new string[] { "설기현""33""1""54" });

            dt.Rows.Add(new string[] { "아이유""28""1""45" });

            dt.Rows.Add(new string[] { "박지성""23""4""10" });

 

            return dt;

        }

    }

}

 

Colored by Color Scripter

cs


실행 결과



위와 같이 데이터가 GridControl에 알맞게 표시되는 것을 확인하실 수 있습니다.

 

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY