none
conditional format of datagridview row (c# and winforms)

    Question

  • Hello.  Have a form that has a datagridview on it.  The dgv is populated with a datatable datasource.  There is a column from the db table in the datatable called DateDue.  In the code below I'm attempting to color the row yellow if the DateDue predates today's date.  Ideally it would be yellow for up to 7 days and red for greater than 7 days.  But will be happy if I can get any of this to work.  So, I seek some help on this for what I'm doing wrong.  Trying to put a routine together as this can be used in more than one place.  Can someone look at this let me know?  Thanks.  John

            private void FormatRow_dgvItemList()
            {
                if (dgvItemList.DataSource != null)
                {
                    foreach (DataGridViewRow dgvRow in dgvItemList.Rows)
                    {
                        if (Convert.ToDateTime(dgvRow.Cells["DateDue"]) < DateTime.Now)
                        {
                            dgvRow.DefaultCellStyle.BackColor = Color.Yellow;
                        }
    
                        else
                        {
                            MessageBox.Show("Error occurred - so fix it");
                        }
                    }
                }
            }
    

    Tuesday, February 05, 2013 2:55 AM

Answers

  • Couldn't stop thinking about why it wasn't working.  So I went back to it used the cellformatting.  The code is posted below.  Just need to tweak it for different lengths of time showing different color....... John

            private void dgvItemList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                foreach (DataGridViewRow row in dgvItemList.Rows)
                {
                    DateTime rowtype = Convert.ToDateTime(row.Cells["DateDue"].Value);
    
                    if (rowtype < DateTime.Now)
                    {
                        dgvItemList.ClearSelection();
                        row.DefaultCellStyle.BackColor = Color.Yellow;
                    }
                }
            }
    

    • Marked as answer by johnboy0276 Tuesday, February 05, 2013 5:37 PM
    Tuesday, February 05, 2013 5:37 PM

All replies

  • Don't use DefaultCellStyle because it is just that:  A default.  By the time the rows have been created the default counts for nothing.  Change the background color of individual cells.  Each cell has a Style property.  Use that.

    Jose R. MCP
    Code Samples

    Tuesday, February 05, 2013 3:14 AM
  • I removed the defaultcellstyle and replaced it with the following 2 lines but still not getting the colored backcolor.

    DataGridViewCell cell = dgvRow.Cells["DateDue"];
    cell.Style.BackColor = Color.Yellow;

    Any other thoughts?


    EDIT/UPDATE:  Place a few breakpoints in and ran thru it again.  When it gets to the 'if (Convert....' line it jumps to opening the form.  Even moved this routine to several other places and whatever is to run after this doesn't.  Have a feeling going about this the wrong way.
    • Edited by johnboy0276 Tuesday, February 05, 2013 4:08 AM
    Tuesday, February 05, 2013 4:00 AM
  • if (Convert.ToDateTime(dgvRow.Cells["DateDue"].Value) < DateTime.Now)


    Jose R. MCP
    Code Samples

    Tuesday, February 05, 2013 4:30 AM
  • It's closer.  Made the change as mentioned above.  When the form loads the 'DateDue' cell is yellow.  But the message appears and cycles thru 5 times and the yellow cell goes back to white.  Just to see what would happen I commented out the 2 replacement lines above and put in - row.DefaultCellStyle.BackColor = Color.Yellow - and the same thing occurs.
    Tuesday, February 05, 2013 4:46 AM
  • Show your code as you have it now and tell me which event you are using to run it.

    Jose R. MCP
    Code Samples

    Tuesday, February 05, 2013 4:52 AM
  • The code is pretty much the same as it was originally posted.  I did remove those 2 lines mentioned above and tried the row.Default... line and got the same result as mentioned above.  This routine is placed inside of another that holds other routines and events.  The holding routine is placed in the form_load event along with other events as needed.  This one is the last of other dgv routines that adjust column order, format the column (width and title), and another that hides columns.  If the one shown here goes ahead of any of these others, they do not run.  Do not spend alot of time on this as too much has been spent on it already.

     private void FormatRow_dgvItemList()
            {
                if (dgvItemList.DataSource != null)
                {
                    foreach (DataGridViewRow row in dgvItemList.Rows)
                    {
                        if (Convert.ToDateTime(row.Cells["DateDue"].Value) < DateTime.Now)
                        {
                            row.DefaultCellStyle.BackColor = Color.Yellow;
                        }
    
                        else
                        {
                            MessageBox.Show("Error occurred - so fix it");
                        }
                    }
                }
            }

    Tuesday, February 05, 2013 5:03 AM
  • Why do you continue to use DefaultCellStyle?  I told you the default style is worthless if the cells already exist.  You need to set the Style.BackColor property of every cell in the row instead. As in:

    if (Convert.ToDateTime(row.Cells["DateDue"].Value) < DateTime.Now)
    {
        foreach (DataGridViewCell c in row.Cells)
        {
            c.Style.BackColor = Color.Yellow;
        }
    }


    Jose R. MCP
    Code Samples

    Tuesday, February 05, 2013 5:22 AM
  •  private void MakeColorsOnDgv()
            {
                for (var i = 0; i < dgvNotif.RowCount; i++)
                {
                    DateTime curdate;
                    try
                    {
                        curdate = Convert.ToDateTime(dgvNotif.Rows[i].Cells[0].Value.ToString());
                    }
                    catch (Exception)
                    {
    
                        curdate = DateTime.Now;
                    }
                    var diff = (curdate.Date - DateTime.Now.Date).Days;
                    if (diff < 3)
                    {
                        dgvNotif.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    }
                    else
                    {
                        if (diff < 7)
                        {
                            dgvNotif.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                        }
                        else
                        {
                            if (diff < 15)
                            {
                                dgvNotif.Rows[i].DefaultCellStyle.BackColor = Color.YellowGreen;
                            }
                            else
                            {
                                if (diff < 30)
                                {
                                    dgvNotif.Rows[i].DefaultCellStyle.BackColor = Color.Green;
                                }
                            }
                        }
                    }
    
                }
                dgvNotif.ClearSelection();
            }
    I recently did similar job.. here is my function.
    Tuesday, February 05, 2013 5:46 AM
  • Thanks to the both of you for responding.  After trying both I still do not get the row coloring.  I'm putting this back on the cutesy list and will see what time still remains at the end of the project to try this again.

    Thanks...John

    Tuesday, February 05, 2013 7:34 AM
  • Couldn't stop thinking about why it wasn't working.  So I went back to it used the cellformatting.  The code is posted below.  Just need to tweak it for different lengths of time showing different color....... John

            private void dgvItemList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                foreach (DataGridViewRow row in dgvItemList.Rows)
                {
                    DateTime rowtype = Convert.ToDateTime(row.Cells["DateDue"].Value);
    
                    if (rowtype < DateTime.Now)
                    {
                        dgvItemList.ClearSelection();
                        row.DefaultCellStyle.BackColor = Color.Yellow;
                    }
                }
            }
    

    • Marked as answer by johnboy0276 Tuesday, February 05, 2013 5:37 PM
    Tuesday, February 05, 2013 5:37 PM