[C# DevExpress] 윈폼 DevExpress GridControl Cell에 Image 넣는 방법


안녕하세요.

 

오늘은 윈폼 DevExpress에서 제공하는 컨트롤인 GridControl에서 CellImage(이미지)를 넣는 방법에 대해서 알려드리려고 합니다.

 

다른 설명 없이 바로 예제 코드를 통해서 어떻게 GridControl Cell에 이미지를 넣는지 보여 드리도록 하겠습니다.


 

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


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



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


그럼 이제 예제 코드를 통해 GridControl Cell에 어떻게 이미지를 넣는지 보도록 할게요.

 

그 전에, 프로젝트 -> 속성에 가셔서 리소스에 테스트할 Image를 아무거나 하나 저장해 주시기 바랍니다.

 

저는 아래와 같이 add 라는 이미지를 저장해 놓았습니다.



그럼 이제 위의 add 이미지를 Cell에 넣어 보도록 하겠습니다.


예제 코드


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

129

130

131

132

133

134

135

136

137

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();

            this.Select();

        }

 

        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);

 

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

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

            {

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

 DevExpress.Utils.HorzAlignment.Center;

            }

 

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

            {

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

 DevExpress.Utils.HorzAlignment.Center;

                uiView_Main.Columns[i].AppearanceHeader.TextOptions.VAlignment =

 DevExpress.Utils.VertAlignment.Center;

            }

 

            //Cell 이벤트 선언

            uiView_Main.RowCellStyle += UiView_Main_RowCellStyle;

            uiView_Main.CustomDrawCell += UiView_Main_CustomDrawCell;

 

            // 처음 열릴 , Row 한줄 추가

            uiView_Main.AddNewRow();

        }

 

        /// <summary>

        /// Custom Draw Cell 이벤트 핸들러

        /// </summary>

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

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

        private void UiView_Main_CustomDrawCell(object sender,

 DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)

        {

            string str = e.CellValue.ToString();

            string PreStr = string.Empty;

 

            if (e.RowHandle != 0)

            {

                PreStr = Convert.ToString(uiView_Main.GetRowCellValue(e.RowHandle - 1, e.Column));

            }

 

            if (str.Trim().Length == 0 && (e.RowHandle == 0 || PreStr.Trim().Length > 0))

            {

                GridView view = (GridView)sender;

                Image icon = Properties.Resources.add; //리소스에 저장한 Image 저장

                e.Graphics.DrawImage(icon, new Rectangle(e.Bounds.X + 

(uiView_Main.Columns[e.Column.AbsoluteIndex].Width) * 2, e.Bounds.Y, 1717));

                e.Appearance.DrawString(e.Cache, e.DisplayText, new Rectangle(e.Bounds.X + 

(uiView_Main.Columns[e.Column.AbsoluteIndex].Width), e.Bounds.Y, e.Bounds.Width - 20, e.Bounds.Height));

                e.Handled = true;

            }

        }

 

        /// <summary>

        /// RowCellStyle 이벤트

        /// 이미지 정렬

        /// </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("Image1");

            dt.Columns.Add("Image2");

            dt.Columns.Add("Image3");

 

            return dt;

        }

    }

}

 

Colored by Color Scripter

cs


실행 결과



위와 같이 제가 리소스에 저장한 add.png 이미지가 각각 Image1, Image2, Image3 컬럼에 알맞게 들어간 것을 보실 수 있습니다.

 

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY