Setting Datagrid column width

SonicBoomAu

Well-known member
Joined
Oct 30, 2003
Messages
179
Location
Australia
Hi All,

What I am tring to find out is if it is possible to set a single columns width during runtime within a datagrid.

For Example.

I load the information straight from an Access database on the form load event. The datagrid has the PreferredColumnWidth set to 100. I want to change the second column width to 300. Is this possible???.

Thanks in advance. :)
 
Anyone.

I know the name of the column. I have read previous threads and they all seem to state that you must name manually set each column name and width. This is what I would prefer not do do as the are some 30+ columns.

Any help is appreciated.
 
After many hours searching the Internet :( , I was able to find where I went wrong. :o

Here is the bit of Code I found:

Code:
        Dim myConnection As New SqlConnection(myConnectString)

        myConnection.ConnectionString = myConnectString
        myConnection.Open()

        Dim da As SqlDataAdapter = New SqlDataAdapter("Select * from tblTest", myConnection)
        Dim ds As New DataSet

        da.Fill(ds, "tbltest")
        DataGrid1.DataSource = ds.Tables("tbltest")

        Changes the datagrid size
        Dim tableStyle1 As New DataGridTableStyle
        tableStyle1.MappingName = "tbltest"
        Me.DataGrid1.TableStyles.Clear()
        Me.DataGrid1.TableStyles.Add(tableStyle1)

        Me.DataGrid1.TableStyles("tbltest").GridColumnStyles(0).Width = 100
        Me.DataGrid1.TableStyles("tbltest").GridColumnStyles(1).Width = 200


        myConnection.Close()

I discovered that you dont need to adjust every column but can place the Columns headertext in the "GridColumnStyles()" area.

I.E. GridColumnStyles(1) becomes GridColumnStyles("Equip0").

Hope this helps the next person. :D
 
Back
Top