[C# FarPoint] C# FarPoint Spread Sheet 컨트롤 Cell Click 이벤트 선언하기(현재 Row 구하기)


안녕하세요.

 

오늘은 C#  FarPoint 두 번째 시간으로서, Spread Sheet Cell Click 이벤트 선언하는 방법에 대해서 알려 드리려고 합니다.

 

FarPoint 컨트롤을 이용하여 작업하다 보면 Cell Click 이벤트를 다루는 경우가 종종 있습니다.

 

저 같은 경우에는 제가 현재 클릭한 Row가 몇 번째 줄인지를 Cell Click 이벤트를 통해서 구현하는 편입니다.


 

그럼 예제 코드를 통해서 직접 Cell Click 이벤트를 선언해서 현재 제가 몇 번째 Row를 클릭했는지 반환하는 프로그램을 만들어 보도록 하겠습니다.


빈 윈폼 프로젝트 선언 및 FarPoint 컨트롤 배치



위와 같이 빈 윈폼 프로젝트에 FarPoint Sheet 컨트롤을 하나 배치 하였습니다.


그럼 이제 Cell Click 이벤트를 선언해서 제가 몇 번째 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

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

138

139

140

141

142

143

144

145

146

147

148

149

150

151

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 Test

{

    public partial class Form1 : Form

    {

        public DataTable dt = null;

        private List<string> ColumnList = new List<string>();

        private int activeRow = 0;

 

 

        public enum COLUMNS

        {

            NAME, AGE, GRADE, PHONE_NUMBER

        }

 

        public Form1()

        {

            InitializeComponent();

 

            this.Load += TestForm_Load;

        }

 

        /// <summary>

        /// Form Load 이벤트 핸들러

        /// </summary>

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

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

        public void TestForm_Load(object sender, EventArgs e)

        {

            InitFrm();

        }

 

        /// <summary>

        /// 설정 메서드

        /// </summary>

        public void InitFrm()

        {

            ColumnList = Enum.GetNames(typeof(COLUMNS)).ToList(); //컬럼명 설정

 

            //테스트 데이터 설정

            GetDataTable();

 

            //Sheet 설정

            SetSheet();

            //Sheet 데이터 넣기

            Set_Data();

        }

 

        /// <summary>

        /// 테스트 데이터 넣기

        /// </summary>

        public void GetDataTable()

        {

            dt = new DataTable(); //DataTable 객체 생성

 

            dt.Columns.Add("NAME"); //컬럼 생성

            dt.Columns.Add("AGE"); //컬럼 생성

            dt.Columns.Add("GRADE"); //컬럼 생성

            dt.Columns.Add("PHONE_NUMBER"); //컬럼 생성

 

            dt.Rows.Add("범범조조""28""4""111-2222-3444");

            dt.Rows.Add("아이유""28""4""112-2812-3444");

            dt.Rows.Add("백예린""24""3""113-5622-044");

            dt.Rows.Add("태연""31""2""114-2222-3404");

        }

 

        /// <summary>

        /// Sheet 설정  컬럼명 설정

        /// </summary>

        private void SetSheet()

        {

            //Cell Click 이벤트 선언

            fpSpread.CellClick -= fpSpread_CellClick;

            fpSpread.CellClick += fpSpread_CellClick;

 

            // Ctrl  다중 선택

            fpSpread.ActiveSheet.SelectionPolicy = 

FarPoint.Win.Spread.Model.SelectionPolicy.MultiRange;

 

            Sheet_Main.SelectionUnit = FarPoint.Win.Spread.Model.SelectionUnit.Row;

            Sheet_Main.OperationMode = FarPoint.Win.Spread.OperationMode.ReadOnly;

 

            // 다중 선택  Color 설정

            Sheet_Main.SelectionStyle = FarPoint.Win.Spread.SelectionStyles.SelectionColors;

            Sheet_Main.SelectionBackColor = Color.FromArgb(255150150255);

 

            Sheet_Main.ColumnHeader.Rows[0].Height = 30;

            Sheet_Main.Columns.Count = dt.Columns.Count;

 

            for (int col = 0; col < Sheet_Main.Columns.Count; col++)

            {

                // 컬럼헤더명

                string tmpColumnHeader = ColumnList[col];

 

                Sheet_Main.ColumnHeader.Cells[0, col].Text =

 tmpColumnHeader.Replace("_"" ");

                Sheet_Main.Columns[col].Width = 150;

 

                // 가운데 정렬

                Sheet_Main.Columns[col].VerticalAlignment = 

FarPoint.Win.Spread.CellVerticalAlignment.Center;

                Sheet_Main.Columns[col].HorizontalAlignment = 

FarPoint.Win.Spread.CellHorizontalAlignment.Center;

            }

 

            for (int row = 0; row < Sheet_Main.Rows.Count; ++row)

                Sheet_Main.Rows[row].BackColor = row % 2 == 1 ? 

Color.FromArgb(255208230252) : Sheet_Main.DefaultStyle.BackColor;

 

            fpSpread.HorizontalScrollBarPolicy = FarPoint.Win.Spread.ScrollBarPolicy.AsNeeded;

            fpSpread.VerticalScrollBarPolicy = FarPoint.Win.Spread.ScrollBarPolicy.AsNeeded;

        }

 

        /// <summary>

        /// Sheet Row 데이터 넣기

        /// </summary>

        public void Set_Data()

        {

            Sheet_Main.Columns.Count = dt.Columns.Count; // Sheet Columns 카운트 설정

            Sheet_Main.Rows.Count = 0//Sheet Row 카운트 설정

 

            for (int row = 0; row < dt.Rows.Count; row++)

            {

                Sheet_Main.Rows.Count++;

 

                for (int col = 0; col < Sheet_Main.Columns.Count; col++)

                {

                    Sheet_Main.Cells[row, col].Value = dt.Rows[row][col].ToString();

                }

            }

        }

 

        /// <summary>

        /// Cell Click 이벤트 핸들러

        /// </summary>

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

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

        public void fpSpread_CellClick(object sender, FarPoint.Win.Spread.CellClickEventArgs e)

        {

            this.activeRow = e.Row; //클릭한 Row  반환

 

            string msg = string.Format("현재 클릭한 Row {0} 번째 줄입니다."this.activeRow + 1);

            MessageBox.Show(msg);

        }

    }

}

 

Colored by Color Scripter

cs


실행 결과



위와 같이 제가 현재 3번째 Row를 클릭했는데, “현재 클릭한 Row3번째 줄입니다.” 라는 메시지 박스가 나온 것을 확인하실 수 있습니다.

 

이렇게 간단히 FarPoint 에서 Cell Click 이벤트 선언해서 사용하는 방법을 알아 보았습니다.

 

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY