프로그래밍/C#
특정셀값 색깔연습
알 수 없는 사용자
2022. 2. 14. 18:00
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = GetData();
this.dataGridView1.DataSource = dt;
//머리글 셀을 포함하여 열에 있는 모든 셀의 내용에 맞도록 열 너비가 조정됩니다.
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
//Cell값을 확인 후 Cell의 스타일 변경
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
foreach(DataGridViewCell cell in row.Cells)
{
if(cell.Value.ToString() == "과학" || cell.Value.ToString() == "수학")
{
DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
cellStyle.BackColor = Color.Blue;
cellStyle.Font = new Font("굴림", 10, FontStyle.Bold);
cell.Style = cellStyle;
}
}
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("과목", typeof(string));
dt.Columns.Add("점수", typeof(int));
dt.Rows.Add("과학", 4235);
dt.Rows.Add("수학", 1540);
dt.Rows.Add("국어", 660);
dt.Rows.Add("영어", 2640);
return dt;
}
}
}