Programming/C#

C# Datagridview 특정 행, 열의 색상 바꾸는 방법

JeongKyun 2022. 3. 29.
반응형

서론

이번 글에서는 Datagridview에서 CellFormatting 이벤트를 사용하여 특정 행,열의 색상을 바꾸고 싶을 때 사용하는 방법을 소개하려한다.

 


 

방법

//ToolBox 설정으로 안할 시 동적으로 이벤트 생성
Datagridview1.CellFormatting += Datagridview1_CellFormatting;

//CellFormatting Event
private void Datagridview1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
	if(e.ColumnIndex == Datagridview1.Columns.Count - 1)
	{
		e.CellStyle.Font = new Font("a고딕15", 14, FontStyle.Regular);
		e.CellStyle.ForeColor = Color.Red;
	}
}

 

필자는 Grid에서 맨 마지막 열의 행 색상을 빨간색으로 바꾸는 방법을 사용했다.

본인 입맛에 맞게 if문 조건에서 ColumnIndex를 지정하여 원하는 열만 적용되게 사용하면 된다.

 


 

결과

댓글

💲 많이 본 글