DataGridView行颜色不会更改

时间:2016-09-06 13:23:37

标签: c# datagridview

您好我正在使用Windows Form Applications但我遇到了问题。我们正在使用数据网格视图,如果一行或更多列是空的行,我想突出显示它。我不知道为什么,但我的代码不起作用。这是我的代码;

 public Form1()
    {
        InitializeComponent();
        var dtCombined = PopulateCombinedDatatable();       
        dataGridView.DataSource = dtCombined;
        HighlateIfEmpty();
    }

    public string[] FindFilePath()
    {
       //OPERATIONS
    }

    public DataTable PopulateCombinedDatatable()
    {

       //MY OPERATIONS
    }

    public void HighlateIfEmpty()
    {
        foreach (DataGridViewRow row in dataGridView.Rows)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                if ((string)cell.Value == string.Empty)
                {
                    cell.Style.BackColor = Color.BlueViolet;
                    cell.Style.SelectionBackColor = Color.Aquamarine;
                    row.DefaultCellStyle.SelectionBackColor = Color.BlueViolet;
                    row.DefaultCellStyle.ForeColor = Color.Yellow;
                    row.DefaultCellStyle.BackColor = Color.Aquamarine;
                }
            }
        }                      
    }

...谢谢

PS:此代码找到正确的列和行但不绘制它

2 个答案:

答案 0 :(得分:1)

您应该在HighlateIfEmpty()事件中调用此dataGridView1_CellFormatting, 为了您的参考,我添加了link,请仔细阅读。

https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx

答案 1 :(得分:0)

我知道这篇文章有点旧,但无论如何。 。 。

在DataGridView上有一个DefaultCellStyle,其中包含SelectionBackColorSelectionForeColor属性。

DataGridView使用样式继承的想法,以防您发现未选择的样式。

相关问题