반응형
DatagridView의 여러가지 Style 속성들을 지정하여 더 보기 좋도록 한다.
private void HideAllColumns()
{
for (int i = 0; i < dgvStudentList.Columns.Count; i++)
{
dgvStudentList.Columns[i].Visible = false;
}
}
private void SetGridDefaultStyle()
{
dgvStudentList.ReadOnly = false;
dgvStudentList.EnableHeadersVisualStyles = false;
dgvStudentList.AllowUserToResizeRows = false;
dgvStudentList.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgvStudentList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgvStudentList.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
dgvStudentList.AllowUserToAddRows = false;
dgvStudentList.BackgroundColor = Color.FromArgb(50, 50, 50);
dgvStudentList.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgvStudentList.ColumnHeadersDefaultCellStyle.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel);
dgvStudentList.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(32, 32, 32);
dgvStudentList.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
dgvStudentList.ColumnHeadersHeight = 30;
dgvStudentList.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
dgvStudentList.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dgvStudentList.RowTemplate.Height = 30;
}
private void ShowColumn(int displayIndex, string columnName, string headerText, bool isReadOnly = true, int columnWidth = 100, bool isAutoSize = false)
{
dgvStudentList.Columns[columnName].DisplayIndex = displayIndex;
dgvStudentList.Columns[columnName].Visible = true;
dgvStudentList.Columns[columnName].Width = columnWidth;
dgvStudentList.Columns[columnName].HeaderText = headerText;
dgvStudentList.Columns[columnName].ReadOnly = isReadOnly;
if (isAutoSize)
{
dgvStudentList.Columns[columnName].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
if (dgvStudentList.Columns[columnName] is DataGridViewImageColumn)
{
((DataGridViewImageColumn)dgvStudentList.Columns[columnName]).DefaultCellStyle.Padding = new Padding(0, 5, 0, 5);
((DataGridViewImageColumn)dgvStudentList.Columns[columnName]).ImageLayout = DataGridViewImageCellLayout.Zoom;
}
}
private void ShowHideColumns()
{
HideAllColumns();
Student student = new Student(null);
ShowColumn(0, nameof(student.NumberInClass), "번호", false, 70);
ShowColumn(1, nameof(student.Name), "이름", false, 100);
ShowColumn(2, nameof(student.BirthDate), "생년월일", true, 150);
ShowColumn(3, nameof(student.classInfo), "School and Class", true, 70, true);
}
private void GUIControlsExample_Load(object sender, EventArgs e)
{
studentsViewModel = new SortableBindingList<Student>();
dgvStudentList.Columns.Clear();
dgvStudentList.DataSource = studentsViewModel;
School school = new School()
{
Name = "Korea Hightschool",
Address = "Seoul"
};
Class classInfo = new Class(school)
{
ClassNumber = 5,
Location = "Second floor",
};
for (int i = 0; i < 20; i++)
{
studentsViewModel.Add(new Student(classInfo)
{
NumberInClass = i + 1,
Name = $"{i + 1}_Ben",
BirthDate = DateTime.Now,
});
}
SetGridDefaultStyle();
ShowHideColumns();
RegisterEvents();
}
적용전 | 적용후 |
반응형
'[====== Development ======] > C#' 카테고리의 다른 글
Visual Studio color schemes (0) | 2021.02.10 |
---|---|
C# DatagridView 조건에 따라 Cell 속성 변경 (0) | 2021.02.09 |
C# Winform Datagridview 의 Datasource에 SortableBindingList 적용 (0) | 2021.02.08 |
Email 전송 기능 (0) | 2021.02.05 |
DateTime Extention Method (0) | 2021.02.05 |