본문 바로가기
C#/DevExpress

C# Grid Row에 번갈아가며 Color 설정하기

by 샤나엘 2020. 10. 23.
반응형

안녕하세요.

아래 그림과 같이 Row에 번갈아가면 색칠하는 방법에 대해 설명드릴게요.

 

 

코드는 아래와 같습니다.

        private void Form1_Load(object sender, EventArgs e)
        {
            //데이터 설정
            List<Data> dataList = GetDataList();
            this.gridControl1.DataSource = dataList;

            GridView gridView = this.gridControl1.MainView as GridView;

            gridView.BestFitColumns();

            //Grid RowStyle 이벤트 설정
            gridView.RowStyle += GridView_RowStyle;
        }

        private void GridView_RowStyle(object sender, RowStyleEventArgs e)
        {
            GridView View = sender as GridView;
            if (e.RowHandle >= 0)
            {
                if (e.RowHandle % 2 == 0)
                {
                    e.Appearance.BackColor = Color.Pink;
                }
            }
        }

중요한 부분은 GridView_RowStyle 이벤트인데요, 여기에서 Row에 색을 칠하게 됩니다.

 

저는 몇번째 Row인지 확인해서 2로 나눠서 나머지 값이 0 이면(짝수 줄이면) 색칠하도록 설정했습니다.

 

Row에 그라데이션 처리를 하고 싶으면 아래와 같이 수정해서 사용하시면 됩니다.

        private void GridView_RowStyle(object sender, RowStyleEventArgs e)
        {
            GridView View = sender as GridView;
            if (e.RowHandle >= 0)
            {
                if (e.RowHandle % 2 == 0)
                {
                    e.Appearance.BackColor = Color.White;
                    e.Appearance.BackColor2 = Color.Pink;
                }
            }
        }

 

반응형

댓글