Click here to Skip to main content
15,882,464 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to change column properties of a bound datagridview in

I found this article with an aswer from OriginalGriff. He says it should work by removing any existing binding first but i can't get it working

What I have tried:

Here is my code, I try to change ColumnHeadersHeight and ColumnHeadersDefaultCellStyle


Public Sub Init_DGV_OverzichtHistorie_2(ByVal DGV_OverzichtHistorie As DataGridView, ByVal Sqlstring As String)

        Dim SqlCmd As MySqlCommand = New MySqlCommand(Sqlstring, Conn)
        Dim Sql_DR As MySqlDataReader
        Dim DT_Historie As New DataTable

			DGV_OverzichtHistorie.DataSource = Nothing
            DGV_OverzichtHistorie.ColumnHeadersHeight = 36
            DGV_OverzichtHistorie.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control


            Sql_DR = SqlCmd.ExecuteReader(CommandBehavior.KeyInfo)
            DGV_OverzichtHistorie.DataSource = Data_Table_Historie

        Catch ex As Exception
        End Try

    End Sub
Updated 15-May-23 9:35am

I just changed my disk and I don't have code already available. But DatagridView has is own Columns collection and Datacolumn object. So you can use Column number and Datacolumn object to do what you want.

You can get Microsoft's documentation there

DataGridViewColumn Class (System.Windows.Forms) | Microsoft Learn[^]
Share this answer
vincentkoert 15-May-23 14:34pm    
Hi cmarcotte, thanks for your reply. I just looked into it and i'm afraid this is not the solution. The problem is that the datagridview is databound, I can't find anything in this article about databound datagridview. I know that you can change the properties of an unbound datagridview very easily. Do I overlook something?
I found the solution myself, I had to add:


DGV_OverzichtHistorie.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
DGV_OverzichtHistorie.EnableHeadersVisualStyles = False

1. Without DisableResizing, the columnheight will not change.
2. Without EnableHeadersVisualStyles = False, the backcolor of the column will not change.

So OriginalGriff was right from the beginning, Set all the properties BEFORE databind the datagridview.

Anyway, thanks for thinking with me.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900