728x90
반응형
728x170
■ DataGridView 클래스에서 셀 색상을 설정하는 방법을 보여준다.
▶ MainForm.cs
namespace TestProject;
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
this.dataGridView.ColumnCount = 4;
this.dataGridView.Columns[0].Name = "도서명";
this.dataGridView.Columns[1].Name = "대출일";
this.dataGridView.Columns[2].Name = "반환일";
this.dataGridView.Columns[3].Name = "기간";
this.dataGridView.Rows.Add("도서 1", "2018/01/13", "2018/01/20");
this.dataGridView.Rows.Add("도서 2", "2018/03/05", "2018/03/13");
this.dataGridView.Rows.Add("도서 3", "2018/04/20", "2018/05/15");
this.dataGridView.Rows.Add("도서 4", "2018/04/18", "2018/05/11");
this.dataGridView.Rows.Add("도서 5", "2018/06/01", "2018/08/22");
this.dataGridView.Rows.Add("도서 6", "2018/05/30", "2018/06/02");
this.dataGridView.Rows.Add("도서 7", "2018/04/04", "2018/05/12");
this.dataGridView.Rows.Add("도서 8", "2018/03/12", "2018/04/10");
Load += Form_Load;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 폼 로드시 처리하기 - Form_Load(sender, e)
/// <summary>
/// 폼 로드시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void Form_Load(object sender, EventArgs e)
{
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.Font = new Font(this.dataGridView.Font, FontStyle.Bold);
style.BackColor = Color.Red;
style.ForeColor = Color.White;
DateTime date1;
DateTime date2;
for(int i = 0; i < this.dataGridView.RowCount - 1; i++)
{
date1 = Convert.ToDateTime(this.dataGridView.Rows[i].Cells[2].Value);
date2 = Convert.ToDateTime(this.dataGridView.Rows[i].Cells[1].Value);
TimeSpan timeSpan = date1 - date2;
this.dataGridView.Rows[i].Cells[3].Value = timeSpan.Days;
if(timeSpan.Days > 10)
{
this.dataGridView.Rows[i].Cells[3].Style = style;
}
}
}
#endregion
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] WebView2 클래스 사용하기 (0) | 2022.10.03 |
---|---|
[C#/WINFORM] ColorTranslator 클래스 : ToHtml 정적 메소드를 사용해 16진수 색상 코드 구하기 (0) | 2022.10.02 |
[C#/WINFORM/.NET6] TimeZoneInfo 클래스 : ConvertTimeFromUtc 정적 메소드를 사용해 UTC 시간을 특정 지역 시간으로 변환하기 (0) | 2022.09.26 |
[C#/WINFORM/.NET6] DataGridView 클래스 : 복수 선택 행 삭제하기 (0) | 2022.08.30 |
[C#/WINFORM/.NET6] DataGridView 클래스 : 단일 선택 행 삭제하기 (0) | 2022.08.30 |
[C#/WINFORM] Control 클래스 : 컨트롤 이벤트 제거하기 (0) | 2022.08.24 |
[C#/WINFORM/.NET6] Point 클래스 : 다각형 내부 위치 여부 구하기 (0) | 2022.08.20 |
[C#/WINFORM/.NET6] Point 클래스 : 다각형 내부 위치 여부 구하기 (0) | 2022.08.20 |
[C#/WINFORM/.NET6] Point 클래스 : 다각형 내부 위치 여부 구하기 (0) | 2022.08.20 |
[C#/WINFORM/.NET6] Point 클래스 : 다각형 내부 위치 여부 구하기 (0) | 2022.08.20 |
댓글을 달아 주세요