[C# 윈폼] C# 윈폼 DataGridView 컨트롤 Image Cell ClickEvent 발생시키기


안녕하세요.

 

오늘은 C# 윈폼에서 DataGridView 컨트롤에 대해서 알아 보려고 합니다.

그 중에서도, 어제에 이어서 Image Cell을 클릭하였을 때 발생되는 CellClickEvent 에 대해서 알려 드리려고 하는데요.

 

어제 예제 코드에 이어서 바로 CellClickEvnet를 어떻게 발생 시키는지 보여드리겠습니다.


 

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



위와 같이 빈 윈폼 프로젝트를 생성해 주시고, DataGridView 컨트롤을 배치해 주시기 바랍니다.

 

그럼 바로, 이미지 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

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 GridViewControlTest

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

 

            // Load 이벤트 선언

            this.Load += LoadForm_Test;

 

            //DataGridView  클릭 이벤트 선언

            this.uiGrid_Main.CellClick += DataGridView_CellClick;

        }

 

        /// <summary>

        ///  Load 이벤트

        /// </summary>

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

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

        public void LoadForm_Test(object sender, EventArgs e)

        {

            SetDataGridImage();

        }

 

        /// <summary>

        /// DataGridView (Column, Row) 설정

        /// </summary>

        public void SetDataGridImage()

        {

            uiGrid_Main.ColumnCount = 3//컬럼 카운트 지정

 

            //컬럼 이름 지정

            GetColumnName();

 

            //Row 데이터 지정

            GetRowData();

 

            //이미지 컬럼 추가

            GetImageColumn();

        }

 

        /// <summary>

        /// 컬럼 이름 지정

        /// </summary>

        public void GetColumnName()

        {

            //컬럼 이름 지정

            uiGrid_Main.Columns[0].Name = "Name";

            uiGrid_Main.Columns[1].Name = "Age";

            uiGrid_Main.Columns[2].Name = "Score";

        }

 

        /// <summary>

        /// Row 데이터 지정

        /// </summary>

        public void GetRowData()

        {

            uiGrid_Main.Rows.Add("이름1""23""52");

            uiGrid_Main.Rows.Add("이름2""33""62");

            uiGrid_Main.Rows.Add("이름3""43""72");

            uiGrid_Main.Rows.Add("이름4""53""82");

            uiGrid_Main.Rows.Add("이름5""23""22");

            uiGrid_Main.Rows.Add("이름6""54""42");

            uiGrid_Main.Rows.Add("이름7""26""26");

            uiGrid_Main.Rows.Add("이름8""38""88");

        }

 

        /// <summary>

        /// 이미지 컬럼 생성

        /// </summary>

        public void GetImageColumn()

        {

            //이미지 컬럼 객체 생성

            DataGridViewImageColumn img = new DataGridViewImageColumn();

 

            //리소스에 저장되어 있는 Image 넣기

            Image _image = Properties.Resources.image1;

            img.Image = _image;

 

            //인덱스 3 , 4번째 컬럼에 image 컬럼 추가

            uiGrid_Main.Columns.Add(img);

            img.HeaderText = "ImageColumn";

            img.Name = "img";

        }

 

        /// <summary>

        /// DataGridView

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

        /// </summary>

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

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

        public void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)

        {

            DataGridView grid = sender as DataGridView;

 

            int curRow = e.RowIndex;

            int curCol = e.ColumnIndex;

 

            //이미지 Column에만 CellClick 반응하도록

            Type _type = grid.Rows[curRow].Cells[curCol].GetType();

 

            //클릭한 Cell 타입이 이미지 컬럼인 경우

            if(_type == typeof(DataGridViewImageCell))

            {

                //해당 Cell 값이 있다면( 이미지가 들어 있다면)

                if(grid.Rows[curRow].Cells[curCol].Value != null)

                {

                    string msg = string.Format("해당 Cell  이미지 Cell 입니다.");

                    MessageBox.Show(msg);

                }

            }

        }

    }

}

 

Colored by Color Scripter

cs

 

실행 결과



위와 같이 다른 컬럼을 클릭하였을 때에는 이벤트가 반응 없다가, 컬럼 Type이 이미지인 Cell을 클릭하였을 때에 Click 이벤트가 동작하시는 것을 확인하실 수 있습니다.

 

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY