Hello,
Is there a way to manually order the columns in a grid.
My grid is bound to the datasource at run-time. So I can't change the order in the designer.
Is there a method to do that in code?
Many thanks,
Steve
Printable View
Hello,
Is there a way to manually order the columns in a grid.
My grid is bound to the datasource at run-time. So I can't change the order in the designer.
Is there a method to do that in code?
Many thanks,
Steve
You can change the order in the designer if you want to, assuming that you know what columns there will. Add the columns you want at design time, put them in the order you want and set their DataPropertyName properties so they know what data source columns to bind to at run time.
Alternatively, once the grid has auto-generated the columns you should be able to use the Remove and Insert methods of the grid's Columns collection, just like you can any other collection, to re-order the items it contains.
Private Sub AdjustColumnOrder()
With customersDataGridView
.Columns("CustomerID").Visible = False
.Columns("ContactName").DisplayIndex = 0
.Columns("ContactTitle").DisplayIndex = 1
.Columns("City").DisplayIndex = 2
.Columns("Country").DisplayIndex = 3
.Columns("CompanyName").DisplayIndex = 4
End With
End Sub
Shweet.