-1

我在 winform 应用程序中有一个 datagridView。我想实现这样的单元格的背景色。

在此处输入图像描述

4

3 回答 3

2
dg.Rows[x].Cells[y].Style.BackColor = Color.Red;
于 2013-09-17T09:49:23.657 回答
0

这是我的演示是:

datagridview1.Rows[0].DefaultCellStyle.BackColor = Color.Yellow;

在你的情况下:

if (data1 == 0 && data2 == "Indirect Expence")
{
  datagridview1.Rows[index].DefaultCellStyle.BackColor = Color.Yellow;
}
于 2013-09-16T08:23:40.937 回答
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;
                    }
                }
            }
于 2013-09-17T09:54:10.523 回答