방법: Windows Forms DataGridView 컨트롤에 대한 기본 셀 스타일 설정
DataGridView 컨트롤을 사용하여 전체 컨트롤과 특정 열 및 행에 대한 기본 셀 스타일을 지정할 수 있습니다. 이러한 기본값은 컨트롤 수준에서 열 수준, 행 수준 및 셀 수준에 이르기까지 필터링합니다. 특정 DataGridViewCellStyle 속성이 셀 수준에서 설정되지 않은 경우 행 수준의 기본 속성 설정이 사용됩니다. 행 수준에서도 속성이 설정되지 않은 경우 기본 열 설정이 사용됩니다. 끝으로, 열 수준에서도 속성이 설정되지 않은 경우 기본 DataGridView 설정이 사용됩니다. 이 설정을 사용하면 여러 수준에서 속성 설정을 복제할 필요가 없습니다. 각 수준에서 상위 수준과 다른 스타일을 지정하면 됩니다. 자세한 내용은 Windows Forms DataGridView 컨트롤의 셀 스타일을 참조하세요.
Visual Studio에서는 이 작업이 광범위하게 지원됩니다. 방법: 디자이너를 사용하여 Windows Forms DataGridView 컨트롤에 기본 셀 스타일 및 데이터 형식 설정도 참조하세요.
프로그래밍 방식으로 기본 셀 스타일을 설정하려면
DataGridView.DefaultCellStyle 속성을 통해 검색된 DataGridViewCellStyle의 속성을 설정합니다.
this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige; this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 12);
Me.dataGridView1.DefaultCellStyle.BackColor = Color.Beige Me.dataGridView1.DefaultCellStyle.Font = New Font("Tahoma", 12)
여러 행과 열에서 사용할 새로운 DataGridViewCellStyle 개체를 만들고 초기화합니다.
DataGridViewCellStyle highlightCellStyle = new DataGridViewCellStyle(); highlightCellStyle.BackColor = Color.Red; DataGridViewCellStyle currencyCellStyle = new DataGridViewCellStyle(); currencyCellStyle.Format = "C"; currencyCellStyle.ForeColor = Color.Green;
Dim highlightCellStyle As New DataGridViewCellStyle highlightCellStyle.BackColor = Color.Red Dim currencyCellStyle As New DataGridViewCellStyle currencyCellStyle.Format = "C" currencyCellStyle.ForeColor = Color.Green
특정 행과 열의
DefaultCellStyle
속성을 설정합니다.this.dataGridView1.Rows[3].DefaultCellStyle = highlightCellStyle; this.dataGridView1.Rows[8].DefaultCellStyle = highlightCellStyle; this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle = currencyCellStyle; this.dataGridView1.Columns["TotalPrice"].DefaultCellStyle = currencyCellStyle;
With Me.dataGridView1 .Rows(3).DefaultCellStyle = highlightCellStyle .Rows(8).DefaultCellStyle = highlightCellStyle .Columns("UnitPrice").DefaultCellStyle = currencyCellStyle .Columns("TotalPrice").DefaultCellStyle = currencyCellStyle End With
예제
private void SetDefaultCellStyles()
{
this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 12);
DataGridViewCellStyle highlightCellStyle = new DataGridViewCellStyle();
highlightCellStyle.BackColor = Color.Red;
DataGridViewCellStyle currencyCellStyle = new DataGridViewCellStyle();
currencyCellStyle.Format = "C";
currencyCellStyle.ForeColor = Color.Green;
this.dataGridView1.Rows[3].DefaultCellStyle = highlightCellStyle;
this.dataGridView1.Rows[8].DefaultCellStyle = highlightCellStyle;
this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle =
currencyCellStyle;
this.dataGridView1.Columns["TotalPrice"].DefaultCellStyle =
currencyCellStyle;
}
Private Sub SetDefaultCellStyles()
Dim highlightCellStyle As New DataGridViewCellStyle
highlightCellStyle.BackColor = Color.Red
Dim currencyCellStyle As New DataGridViewCellStyle
currencyCellStyle.Format = "C"
currencyCellStyle.ForeColor = Color.Green
With Me.dataGridView1
.DefaultCellStyle.BackColor = Color.Beige
.DefaultCellStyle.Font = New Font("Tahoma", 12)
.Rows(3).DefaultCellStyle = highlightCellStyle
.Rows(8).DefaultCellStyle = highlightCellStyle
.Columns("UnitPrice").DefaultCellStyle = currencyCellStyle
.Columns("TotalPrice").DefaultCellStyle = currencyCellStyle
End With
End Sub
코드 컴파일
이 예제에는 다음 사항이 필요합니다.
dataGridView1
이라는 DataGridView 컨트롤System, System.Drawing 및 System.Windows.Forms 어셈블리에 대한 참조
강력한 프로그래밍
큰 데이터 집합으로 작업할 때 최대 확장성을 얻으려면 개별 요소에 대한 스타일 속성을 별도로 설정하는 대신 동일한 스타일을 사용하는 여러 행, 열 또는 셀에서 DataGridViewCellStyle 개체를 공유해야 합니다. 또한 공유 행을 만들고 DataGridViewRowCollection.SharedRow 속성을 통해 액세스해야 합니다. 자세한 내용은 Windows Forms DataGridView 컨트롤의 크기를 조정하는 최선의 방법을 참조하세요.
참고 항목
.NET Desktop feedback
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기