DataGridView set row header color


  • I have a win form with an unbound DataGridView on it. Due to the nature of the data being displayed in it, everything is manually built programmatically. I have data base with employees that I retrieve then loop through to build all the rows in the grid. I need to have each employee's row header a different color, depending on thier assigned department. However, when building the rows when I set the HeaderCell.Style.BackColor attribute, it doesn't take. For that matter, if I set the Back Color attribute for the DataGridView DefaultRowHeaderStyle from the VS designer that won't take either. Same thing applies to columns. No Header styles seem to apply (colors, fonts, spacing, etc).

    Can anyone give me an idea why I can't change the styles?

    Here's my code that produces my Employee rows in the data grid...

    // loop through the employee list and create a row for each one
    foreach (Employee thisEmployee in employeeList)
        // create a row for this employee
        using (DataGridViewRow employeeRow = new DataGridViewRow())
            // set the properties for this row and row header
            employeeRow.HeaderCell.Style.BackColor = Color.FromArgb(thisEmployee.Department.ColorCode); <---- DOESN'T CHANGE THE COLOR???
            employeeRow.HeaderCell.Value = thisEmployee.FullNameReversed;
            employeeRow.HeaderCell.ToolTipText = String.Format("Employee #: {0}\nName: {1}", thisEmployee.EmployeeNumber, thisEmployee.FullName);
            employeeRow.Tag = thisEmployee;

            // add the row

    Tuesday, August 22, 2006 3:07 PM


All replies