tags:

views:

103

answers:

2

So I'm adding columns in the code, rather than design view...

frmMain.dgv_test.Columns.Add("col1", "1")
frmMain.dgv_test.Columns.Add("col2", "2")
'etc

How do I edit properties such as Column Width, Frozen, and all the other properties that can be seen in the design view if I were to "design" a column?

Thank you.

A: 

Create a new Temp DataGridColumn then set all the properties you want for that column and then add it to the grid.

Dim tempC as new DataGridColumn()

tempC.HeaderText ="col1"
tempC.HeaderStyle.whatever
etc....

...then

frmMain.dgv_test.Collumns.Add(tempC)

http://msdn.microsoft.com/en-us/library/2wfbzezz%28v=VS.100%29.aspx

ANC_Michael
A: 

The DataGridViewColumnCollection.Add method actually returns the index of the added DataGridViewColumn, so you can also do this:

Dim colIndex As Integer = frmMain.dgv_test.Columns.Add("col1", "1")
Dim col As DataGridViewColumn = frmMain.dgv_test.Columns(colIndex)
col.Width = 100
col.Frozen = True

Or here's another, less verbose way:

With frmMain.dgv_test.Columns
    Dim col As DataGridViewColumn = .Item(.Add("col1", "1"))
    col.Width = 100
    col.Frozen = True
End With

And so on.

Dan Tao