Berkin
Berkin

Reputation: 1664

DataGridView Row Color Doesn't Change

Hello im working on Windows Form Applications but i have a problem. We are using data grid view and if row's if one column or more is empty, i want to highlight it. I dont know why but my code doesn't work. Here its my code;

 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;
                }
            }
        }                      
    }

Thanks...

PS : This code finds right columns and rows but not paint it

Upvotes: 3

Views: 1234

Answers (2)

user7075507
user7075507

Reputation:

I know this post is a bit old, but anyway . . .

On the DataGridView there is a DefaultCellStyle, inside this there is SelectionBackColor and SelectionForeColor properties.

The DataGridView uses a style inheritance idea, in case you find that the style you pick is not being applied.

Upvotes: 0

Subash B
Subash B

Reputation: 164

You should call this HighlateIfEmpty() in dataGridView1_CellFormatting event, for your reference i added a link please go through it.

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

Upvotes: 2

Related Questions