C#Datagridview-每行行后颜色不起作用

时间:2018-10-23 10:15:53

标签: c# winforms datagridview

我的访问数据库上有三个复选框,并且根据要选中的复选框,我希望Datagridview中的每一行都是一种特定的颜色。

  
      
  • 真,假,假-行应为蓝色
  •   
  • 错误,错误,错误-行应为黄色
  •   
  • 正确,正确,错误-行应为红色
  •   
  • 真,假,真-行应为绿色
  •   

当我过滤数据库时,下面的代码似乎可以正常工作,但在查看所有条目时却无法正常工作(它会使所有行变成一种颜色[黄色])。

这是我到目前为止编写的代码:

private void sJDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
                {
                    Color viewModeColour = Color.White; // Default Colour
                    Color viewModeColourRGB = Color.FromArgb(viewModeColour.R, viewModeColour.G, viewModeColour.B);
                    foreach (DataGridViewRow row in sJDataGridView.Rows)
                    {
                        if (Convert.ToBoolean(row.Cells[14].Value) == true)
                        {
                            if (Convert.ToBoolean(row.Cells[15].Value) == true)
                            {
                                viewModeColour = Color.FromArgb(0xFF8787); // Red
                            }
                            else
                            {
                                if (Convert.ToBoolean(row.Cells[16].Value) == false)
                                {
                                    viewModeColour = Color.FromArgb(0xE3F7FF); // Blue
                                }
                                else
                                {
                                    viewModeColour = Color.FromArgb(0xE5FFCC); // Green
                                }
                            }
                        }
                        else
                        {
                            if (Convert.ToBoolean(row.Cells[15].Value) == false)
                            {
                                if (Convert.ToBoolean(row.Cells[16].Value) == false)
                                {
                                    viewModeColour = Color.FromArgb(0xFFFDCC); // Yellow
                                }
                            }
                        }
                        viewModeColourRGB = Color.FromArgb(viewModeColour.R, viewModeColour.G, viewModeColour.B);
                    }
         
                    for (int i = 0; i < sJDataGridView.Rows.Count; i++)
                    {
                        sJDataGridView.Rows[i].DefaultCellStyle.BackColor = viewModeColourRGB;
                    }    
                     
                }
            }

1 个答案:

答案 0 :(得分:2)

尝试在不同的if语句块中检查每个条件。

然后在您的DefaultCellStyle块中应用foreach

foreach (DataGridViewRow row in sJDataGridView.Rows)
{
    bool a = Convert.ToBoolean(row.Cells[14].Value);
    bool b = Convert.ToBoolean(row.Cells[15].Value);
    bool c = Convert.ToBoolean(row.Cells[16].Value);

    if (a == true && b == false && c == false)
        viewModeColour = Color.FromArgb(0xE3F7FF); // Blue

    if (a == false && b == false && c == false)
        viewModeColour = Color.FromArgb(0xFFFDCC); // Yellow

    if (a == true && b == true && c == false)
        viewModeColour = Color.FromArgb(0xFF8787); // Red

    if (a == true && b == false && c == true)
        viewModeColour = Color.FromArgb(0xE5FFCC); // Green

    viewModeColourRGB = Color.FromArgb(viewModeColour.R, viewModeColour.G, viewModeColour.B);
    row.DefaultCellStyle.BackColor = viewModeColourRGB;
}