Click here to Skip to main content
15,884,353 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 vb.net

I found this article https://www.codeproject.com/Questions/5271032/Columncount-property-cannot-be-set-on-a-data-bound 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

code:

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

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

            Conn.Open()

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

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Conn.Close()
            Conn.Dispose()
        End Try

    End Sub
Posted
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
 
Comments
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:

VB.NET

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
 
v2

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