none
COLOURS IN DATAGRIDVIEW RRS feed

  • Question

  • I tried the example provided in the Library for the System.Windows.Forms.DataGridView class and found that the program ignores lines:

            songsDataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;     //** IGNORED
            songsDataGridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;    //** IGNORED

    The column headers display with black text on white foreground. I tried different colors, with no result.

    When the mouse hovers on the column headers, the cell is highlighted LightBlue, but it becomes white again when the mouse moves away.

    Conversely, in a program of my own (where the properties of dataGridView1 are set by means of the wizard), one of the column headers remains always highlighted, and it identifies the column of the selected cell. I would like to get rid of this indication, I prefer that the header remains always the same color - of my own choice.

    I am confused by the very many properties for CellStyle, DefaultCellStyle, etc. and cannot sort out a solution to obtain the appearance that I want.




    • Edited by GiorgioITA Sunday, March 31, 2019 2:08 PM
    Sunday, March 31, 2019 2:08 PM

Answers

All replies

  • Hi GiorgioITA,

    Thank you for posting here.

    For your question, if you want to change the column header color of datagridview, you could try the code below.

     dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Gray;
                dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.Green;
                dataGridView1.EnableHeadersVisualStyles = false;

    If you do not set the EnableHeadersVisualStyles flag to False, then the changes you make to the style of the header will not take effect.

    For more details, you could refer to the MSDN document.

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.enableheadersvisualstyles?redirectedfrom=MSDN&view=netframework-4.7.2#System_Windows_Forms_DataGridView_EnableHeadersVisualStyles

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, April 1, 2019 2:44 AM
    Moderator
  • Thank you for your swift reply.

    Now I can set the colors of my choice! The second part of my original query remains open: one of the cells of the column header is always highlighted, precisely the column that contains the selected cell. However, I found a simple trick to fix this:

                dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Bisque;
                dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black;
                dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font(dataGridView1.Font, FontStyle.Bold);
                dataGridView1.ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.Bisque;
                dataGridView1.EnableHeadersVisualStyles = false;    //** Thanks Wendy

    Making the SelectionBackColor same as the 'normal' BackColor, in fact the selection becomes invisible.

    Perhaps there are other methods to achieve the same result?




    • Edited by GiorgioITA Monday, April 1, 2019 1:52 PM
    Monday, April 1, 2019 1:52 PM
  • Hi GiorgiolTA,

    Thanks for your sharing.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 2, 2019 2:07 AM
    Moderator