DataGridView Question in VB: Header

Con risposta DataGridView Question in VB: Header

  • lunedì 16 aprile 2012 12:56
     
     

    Hi everybody,

    I would like to modify the backcolor of the header of one spezific column in a datagridview (DGV) in VB and I would be greatful for any tips that will help me do that.

    Thanks

    Best regards - Walter


    wafest42

Tutte le risposte

  • lunedì 16 aprile 2012 13:27
     
      Contiene codice

    hi,

    songsDataGridView.Columns("Title").DefaultCellStyle.BackColor = Color.Red


    Regards, Nico

  • lunedì 16 aprile 2012 13:37
     
     Con risposta Contiene codice

    Walter,

    to change the backcolor of a header of a column, you first need to set the EnableHeadersVisualStyles property of the DGV to false.

    You can try the code below:

    Public Class Form1
        Private tList As New TestList
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim bdStyle As New DataGridViewCellStyle
            bdStyle.BackColor = Color.Red
            DataGridView1.Columns(0).HeaderCell.Style = bdStyle
        End Sub
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            DataGridView1.EnableHeadersVisualStyles = False
            tList.Add(New Test)
            DataGridView1.DataSource = tList
        End Sub
    End Class
    Public Class Test
        Private _test As String
        Public Property Caption() As String
            Get
                Return "test"
            End Get
            Set(ByVal value As String)
                _test = value
            End Set
        End Property
    End Class
    Public Class TestList
        Inherits List(Of Test)
    End Class

    Just create a new WinFor Project, add a Button and a DatagridView and paste this code inside the code editor.

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

  • martedì 17 aprile 2012 19:44
     
     

    Thank you for your tip, Nico, but somehow it did not do it for me.

    If I interpret your tip correctly, it would probably color the entire title header red, which is not what I want. I want to color the header of each column separately.

    I just tried some of the commands of Hannes Heslacher's reply and they did what I wanted.

    Thanks again - Walter


    wafest42

  • martedì 17 aprile 2012 19:51
     
     

    Thank you, Hannes, for your help: that did it for me!

    Very important was the following, which I set in the DGV property.

    DataGridView1.EnableHeadersVisualStyles = False

    The next steps were:

            Dim bdStyle As New DataGridViewCellStyle
            bdStyle.BackColor = Color.Red
            DataGridView1.Columns(0).HeaderCell.Style = bdStyle

    which will address each column header by changing its index.

    Great, thanks again

    Best regards - Walter


    wafest42