[C# DevExpress] C# DevExpress GridControl(그리드컨트롤)에서 특정 행 Color(색상) 변경하기


안녕하세요.

 

오늘은 C# DevExpress에서 GridControl 다루는 방법에 대해서 알아보려고 하는데요.

 

그 중에서요, GridControl에서 특정 행을 선택했을 시, Color(색상) 변경하는 방법에 대해서 알려드리려고 합니다.

 

그럼 바로, 예제 코드를 통해서 알아보도록 하겠습니다.


 

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


윈폼 프로젝트 생성 및 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

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

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;

 

            //Row Cell 스타일 이벤트 선언

            gv.RowCellStyle += DevForm_RowCellStyle;

        }

 

        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;

        }

 

        /// <summary>

        /// 특정  색상 변경 이벤트 핸들러

        /// </summary>

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

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

        public void DevForm_RowCellStyle(object sender, RowCellStyleEventArgs e)

        {

            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

 

            GridView view = sender as GridView;

            if (view == null)

                return;

 

            if (e.RowHandle != view.FocusedRowHandle)

            {

                if (e.Column.Name.Contains("Name"))

                {

                    e.Appearance.ForeColor = Color.Blue;

                }

                else if (e.Column.Name.Contains("Grade"))

                {

                    e.Appearance.ForeColor = Color.Red;

                }

            }

 

            var row = this.gridView1.GetRowCellDisplayText(e.RowHandle, "Name");

 

            if (row.Equals("범범조조"))

            {

                e.Appearance.BackColor = Color.OrangeRed;

            }

        }

    }

}

 

Colored by Color Scripter

cs


실행 결과



위와 같이 특정 Cell에서 Color가 변경된 것을 확인하실 수 있습니다.

 

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY