Datagridview won't change cell color

Richard

I have tried everything to make this change colors it seems. I have read through a lot of stackoverflow posts and have not found my answer. The colors changed before I made the datatable populate data from the database. After that they quit changing colors. Here is my code that is firing on CellFormatting event.

        public ScheduleUserControl()
    {
        InitializeComponent();
        dataGridView.CellClick += (s, e) => OnCellClick(e.ColumnIndex, e.RowIndex);
        dataGridView.CellFormatting += (s, e) => CellFormating(e.ColumnIndex, e.RowIndex);
        btnAll.Click += (s, e) => Filter(Filters.All);
        btnHourly.Click += (s, e) => Filter(Filters.Hourly);
        btnSalary.Click += (s, e) => Filter(Filters.Salary);
    }

private void CellFormating(int c, int r)
    {
        var cell = dataGridView[c, r];
        var tagObject = cell.Tag;
        if (tagObject == null)
        {
            cell.Style.ForeColor = Color.Black;
            cell.Style.BackColor = Color.White;
        }
        else
        {
            var tagType = tagObject.GetType();
            if (tagType == typeof(DayOff))
            {
                var avail = (DayOff)tagObject;
                if (avail != null)
                {
                    cell.Style.ForeColor = Color.White;
                    cell.Style.BackColor = Color.Firebrick;
                }
            }
            else
            if (tagType == typeof(DayOffRequest))
            {
                var request = (DayOffRequest)tagObject;
                if (request.Status == DayOffRequest.RequestStatus.Approved)
                {
                    cell.Style.ForeColor = Color.Black;
                    cell.Style.BackColor = Color.SkyBlue;
                }
                else
                if (request.Status == DayOffRequest.RequestStatus.Pending)
                {
                    cell.Style.ForeColor = Color.Black;
                    cell.Style.BackColor = Color.LightGoldenrodYellow;
                }
            }
            else
            if (tagType == typeof(Shift))
            {
                cell.Style.ForeColor = Color.White;
                cell.Style.BackColor = Color.Green;
            }
        }
    }

Any help would be greatly appreciated it. Debugging and stepping through it says it executes the cell.style changes but the datagridview just doesn't show those changes.

SᴇM

You are creating recursion by changing cell style in CellFormating() method and it can cause unexpected results. So try to set your cell styles in different event, for example in DataBindingComplete.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

DataGridView Mousedown and Mouseup cell background color change doesn't work

From Dev

Datagridview Change cell color of non empty cell

From Dev

change color of cell of dynamicly created datagridview

From Dev

Datagridview checkbox cell change color of button and drawline

From Dev

Change color of only part of text inside a DataGridView cell

From Dev

iOS button title color won't change

From Dev

Jumbotron color and background won't change

From Dev

Background color won't change in panel

From Dev

Why Won't the Screen Change Color

From Dev

Mouseover on div won't change color

From Dev

SKView/SKScene won't change background color

From Dev

Bootstrap button highlight color won't change

From Dev

Mouseover on div won't change color

From Dev

Jumbotron color and background won't change

From Dev

Background color won't change javascript

From Dev

Change of the edited cell in a datagridview cell

From Dev

DataGridView won't load

From Dev

Title color of UIButton won't change on highlighted/selected but background color will

From Dev

Change background color on a cell if that cell doesn't have a number entered

From Dev

Datagridview Rows change color

From Dev

Changed datagridview cell backgroung color

From Dev

Changing datagridview cell color dynamically

From Dev

DataGridView cell color not changing on error

From Dev

TableViewCell subtitle won't change unless the cell is tapped

From Dev

willDisplayCell won't change background color consistent with logic

From Dev

Body background-color won't change while scrolling

From Dev

Android Material: Status bar color won't change

From Dev

Why won't my JFrame program change background color?

From Dev

Body background-color won't change while scrolling

Related Related

  1. 1

    DataGridView Mousedown and Mouseup cell background color change doesn't work

  2. 2

    Datagridview Change cell color of non empty cell

  3. 3

    change color of cell of dynamicly created datagridview

  4. 4

    Datagridview checkbox cell change color of button and drawline

  5. 5

    Change color of only part of text inside a DataGridView cell

  6. 6

    iOS button title color won't change

  7. 7

    Jumbotron color and background won't change

  8. 8

    Background color won't change in panel

  9. 9

    Why Won't the Screen Change Color

  10. 10

    Mouseover on div won't change color

  11. 11

    SKView/SKScene won't change background color

  12. 12

    Bootstrap button highlight color won't change

  13. 13

    Mouseover on div won't change color

  14. 14

    Jumbotron color and background won't change

  15. 15

    Background color won't change javascript

  16. 16

    Change of the edited cell in a datagridview cell

  17. 17

    DataGridView won't load

  18. 18

    Title color of UIButton won't change on highlighted/selected but background color will

  19. 19

    Change background color on a cell if that cell doesn't have a number entered

  20. 20

    Datagridview Rows change color

  21. 21

    Changed datagridview cell backgroung color

  22. 22

    Changing datagridview cell color dynamically

  23. 23

    DataGridView cell color not changing on error

  24. 24

    TableViewCell subtitle won't change unless the cell is tapped

  25. 25

    willDisplayCell won't change background color consistent with logic

  26. 26

    Body background-color won't change while scrolling

  27. 27

    Android Material: Status bar color won't change

  28. 28

    Why won't my JFrame program change background color?

  29. 29

    Body background-color won't change while scrolling

HotTag

Archive