我在winform应用程序中有一个datagridView。我希望像这样实现细胞的背景色。
答案 0 :(得分:2)
dg.Rows[x].Cells[y].Style.BackColor = Color.Red;
答案 1 :(得分:0)
这是我的演示:
datagridview1.Rows [0] .DefaultCellStyle.BackColor = Color.Yellow;
在你的情况下:
if (data1 == 0 && data2 == "Indirect Expence")
{
datagridview1.Rows[index].DefaultCellStyle.BackColor = Color.Yellow;
}
答案 2 :(得分:0)
下面是我现在使用的一些代码。这应该对你有帮助。
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;
}
}
}