본문 바로가기
C#/DevExpress

[C# DevExpress] GridControl 특정 Row, Cell 색 변경

by 샤나엘 2018. 4. 12.
반응형


C# DevExpress GridControl의 특정 Row, 특정 Cell의 색을 변경하는 예제입니다.

 

[특정 Row Color 변경]

 

 

- 소스코드

GridView에 RowStyle 이벤트를 추가하는 부분과

gridView1_RowStyle() 함수를 참고하시면 됩니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private void Form1_Load(object sender, EventArgs e)
        {
            DataTable table = CreateData();
            
            this.gridControl1.DataSource = table;
 
            //GridView에 RowStyle 이벤트 추가
            (this.gridControl1.MainView as GridView).RowStyle += gridView1_RowStyle;
        }
 
        private void gridView1_RowStyle(object sender, RowStyleEventArgs e)
        {
            GridView View = sender as GridView;
            if (e.RowHandle >= 0)
            {
                //컬럼 값이 PRODUCT인 컬럼의 Cell 값을 찾아옴
                string product = View.GetRowCellDisplayText(e.RowHandle, View.Columns["PRODUCT"]);
                if (product == "APPLE")//PRODUCT에 해댱하는 CELL 값이 APPLE일 경우 Row색 변경
                {
                    e.Appearance.BackColor = Color.YellowGreen;
                    e.Appearance.BackColor2 = Color.White;//그라데이션 처리
                }
            }
        }

 

[특정 Cell Color 변경]

 

 

- 소스코드

RowCellStyle 이벤트를 추가하는 부분과

gridView1_RowCellStyle() 함수를 참고하시면 됩니다.

 
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
private void Form1_Load(object sender, EventArgs e)
        {
            DataTable table = CreateData();
            
            this.gridControl1.DataSource = table;
 
            //GridView에 RowCellStyle 이벤트 추가
            (this.gridControl1.MainView as GridView).RowCellStyle += gridView1_RowCellStyle;
        }
 
        private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            GridView view = sender as GridView;
            if (view == null)
                return;
 
            if (e.RowHandle != view.FocusedRowHandle)
            {
                if (e.CellValue == "APPLE")//Cell의 값이 APPLE인 경우 Cell색 변경
                {
                    e.Appearance.BackColor = Color.Pink;
                    e.Appearance.BackColor2 = Color.White; //그라데이션 처리
                }
            }
        }



※ 팁

 

AutoFilterRow 설정방법(아래 그림에서 빨간색으로 표시한 부분)

 

 

 

- 소스코드

GridView의 OptionsView.ShowAutoFilterRow 값을 true로 설정해주면 됩니다.

1
2
3
4
5
6
7
8
9
        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable table = CreateData();
            
            this.gridControl1.DataSource = table;
            (this.gridControl1.MainView as GridView).OptionsView.ShowAutoFilterRow = true;
            //GridView에 RowStyle 이벤트 추가
            (this.gridControl1.MainView as GridView).RowStyle += gridView1_RowStyle;
        }

 

[검색화면]

 

 


반응형

댓글