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

    DN Admin
    Expand Collapse
    Staff Member

    Country:
    United States
    Joined:
    Aug 7, 2010
    Likes Received:
    258
    Trophy Points:
    45
    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 style="color:Blue; For <span style="color:Blue; Each rw <span style="color:Blue; As DataRow <span style="color:Blue; In <span style="color:Blue; Me.dtUserSettings.Rows

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

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

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

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

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

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

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

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

    View the full article
     

Share This Page