Datagridview颜色变化
问题描述:
我试图根据单元格中的值更改颜色,完成或不完整,但由于某种原因,它说“颜色”在当前上下文中不存在。Datagridview颜色变化
是否有我应该使用的系统项目或什么?
如果任何人有任何替代我想要做的,也将不胜感激。
foreach (DataGridViewRow row in dtaFinished.Rows)
{
string RowType = row.Cells[4].Value.ToString();
if (RowType == "Completed")
{
row.DefaultCellStyle.BackColor = Color.Green; //Error on these lines
row.DefaultCellStyle.ForeColor = Color.White; //Error on these lines
}
else if (RowType == "Incomplete")
{
row.DefaultCellStyle.BackColor = Color.Yellow;
row.DefaultCellStyle.ForeColor = Color.Black;
}
}
答
使用下面的命名空间:
using System.Drawing;
希望这会有所帮助。
答
我在一个项目中使用这个前阵子: -
private void dgvOutstandingReports_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex >= 0 && colIndex >= 0)
{
DataGridViewRow theRow = dgvOutstandingReports.Rows[rowIndex];
if (theRow.Cells[colIndex].Value.ToString() == "Daily Report")
{
theRow.DefaultCellStyle.BackColor = Color.LightYellow;
}
else if (theRow.Cells[colIndex].Value.ToString() == "Monthly Report")
{
theRow.DefaultCellStyle.BackColor = Color.LightGray;
}
else if (theRow.Cells[colIndex].Value.ToString() == "SMP Report")
{
theRow.DefaultCellStyle.BackColor = Color.Snow;
}
else if (theRow.Cells[colIndex].Value.ToString() == "Weekly Report")
{
theRow.DefaultCellStyle.BackColor = Color.Pink;
}
else if (theRow.Cells[colIndex].Value.ToString() == "Hourly Report")
{
theRow.DefaultCellStyle.BackColor = Color.LightSteelBlue;
}
}
}