for (int i = 0; i < dataGridView1.RowCount; i++)
{
if (dataGridView1.Rows[i].Cells["State"].Value.ToString() == "Yes")
{
dataGridView1.Rows[i].Cells["State"].Style.BackColor = Color.BlueViolet;
}
}
另一例:根據年齡
void CellColor2()
{
try
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
int m_Y1 = DateTime.Parse(dataGridView1.Rows[i].Cells["Birthday"].Value.ToString()).Year;
int m_Y2 = DateTime.Now.Year;
int m_age = m_Y2 - m_Y1;
if (m_age <35)
{
dataGridView1.Rows[i].Cells["年齡"].Style.BackColor = Color.OliveDrab;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "例外訊息");
}
}
沒有留言:
張貼留言