Change Font Color of DataGridView Column Header Text for Read Only Columns

Discussion in 'Visual Basic .Net' started by DN Admin, Jan 9, 2012.

  1. DN Admin

    Staff Member

    Country:
    United States
    Joined:
    Aug 7, 2010
    Location:
    Joliet, IL U.S.A.
    I am having an issue trying to figure out how to change the font color of the column header text in my datagridview if the entire column is read-only. I basically use a loop to add columns to my datagridview and then set the properties of the columns.
    But for some reason setting the DefaultCellStyle.ForeColor and HeaderCell.Style.ForeColor properties does not work. The header text is still black. How can I fix this?

    <div style="color:Black;background-color:White; <pre>
    <span style="color:Green; ' loop thru each row in user settings table and add columns and set column properties</span>
    <span style="color:Blue; For</span> <span style="color:Blue; Each</span> rw <span style="color:Blue; As</span> DataRow <span style="color:Blue; In</span> <span style="color:Blue; Me</span>.dtUserSettings.Rows

    <span style="color:Green; ' add the column to the datagridview</span>
    <span style="color:Blue; Select</span> <span style="color:Blue; Case</span> rw(<span style="color:Blue; Me</span>.intType)
    <span style="color:Blue; Case</span> <span style="color:Blue; Is</span> = <span style="color:#A31515; "Text"</span> : dgv.Columns.Add(<span style="color:Blue; New</span> DataGridViewTextBoxColumn)
    <span style="color:Blue; Case</span> <span style="color:Blue; Is</span> = <span style="color:#A31515; "Link"</span> : dgv.Columns.Add(<span style="color:Blue; New</span> DataGridViewLinkColumn)
    <span style="color:Blue; Case</span> <span style="color:Blue; Is</span> = <span style="color:#A31515; "CheckBox"</span> : dgv.Columns.Add(<span style="color:Blue; New</span> DataGridViewCheckBoxColumn)
    <span style="color:Blue; Case</span> <span style="color:Blue; Is</span> = <span style="color:#A31515; "ComboBox"</span> : dgv.Columns.Add(<span style="color:Blue; New</span> DataGridViewComboBoxColumn)

    <span style="color:Green; 'Case Is = "Button"</span>
    <span style="color:Green; 'Case Is = "Image"</span>
    <span style="color:Blue; Case</span> <span style="color:Blue; Else</span>
    <span style="color:Green; ' give the primary key of the row where the column type is not recognized</span>
    <span style="color:Blue; Throw</span> <span style="color:Blue; New</span> ApplicationException(<span style="color:#A31515; "Unrecognized Column Type in DataGridViewSettings; Primary Key: "</span> & rw(0).ToString)
    <span style="color:Blue; End</span> <span style="color:Blue; Select</span>

    <span style="color:Green; ' set the new columns properties</span>
    <span style="color:Blue; With</span> dgv.Columns(dgv.Columns.<span style="color:Blue; Count</span> - 1)
    .DataPropertyName = rw(<span style="color:Blue; Me</span>.intPropertyName).ToString
    .Name = rw(<span style="color:Blue; Me</span>.intPropertyName).ToString
    .HeaderText = rw(<span style="color:Blue; Me</span>.intHeaderText).ToString
    .DisplayIndex = rw(<span style="color:Blue; Me</span>.intDisplayIndex)
    .Width = rw(<span style="color:Blue; Me</span>.intWidth)

    .<span style="color:Blue; ReadOnly</span> = rw(<span style="color:Blue; Me</span>.intReadOnly)
    <span style="color:Green; ' change font color of read-only columns</span>
    <span style="color:Blue; If</span> .<span style="color:Blue; ReadOnly</span> <span style="color:Blue; Then</span>
    .DefaultCellStyle.ForeColor = Color.DarkSlateGray
    .HeaderCell.Style.ForeColor = Color.DarkSlateGray
    <span style="color:Blue; End</span> <span style="color:Blue; If</span>

    .Visible = rw(<span style="color:Blue; Me</span>.intVisible)

    <span style="color:Blue; If</span> .<span style="color:Blue; GetType</span>.Name = <span style="color:#A31515; "DataGridViewComboBoxColumn"</span> <span style="color:Blue; Then</span>
    <span style="color:Blue; DirectCast</span>(dgv.Columns(dgv.Columns.<span style="color:Blue; Count</span> - 1), DataGridViewComboBoxColumn).DisplayStyle = DataGridViewComboBoxDisplayStyle.<span style="color:Blue; Nothing</span>
    <span style="color:Blue; End</span> <span style="color:Blue; If</span>

    <span style="color:Blue; End</span> <span style="color:Blue; With</span>

    <span style="color:Blue; Next</span> rw
    </pre>
    <br/>
    Thanks in advance!
    <hr class="sig Ryan

    View the full article
     
    #1

Share This Page