The column order of the DataGridView does not seem to work

I have a DataGridView associated with a list of business objects:

Dim template As New IncidentTemplate Dim temps As List(Of IncidentTemplate) = template.LoadAll Dim bs As New BindingSource If Not temps Is Nothing Then bs.DataSource = temps Me.dgvTemplates.DataSource = bs End If 

Then I add a column with unlinked buttons and make some invisible linked columns:

  Dim BtnCol As New DataGridViewButtonColumn With BtnCol .Name = "Edit" .Text = "Edit" .HeaderText = String.Empty .ToolTipText = "Edit this Template" .UseColumnTextForButtonValue = True End With .Columns.Add(BtnCol) BtnCol = Nothing For Each col As DataGridViewColumn In Me.dgvTemplates.Columns Select Case col.Name Case "Name" col.DisplayIndex = 0 col.FillWeight = 100 col.Visible = True Case "Description" col.DisplayIndex = 1 col.FillWeight = 100 col.Visible = True Case "Created" col.DisplayIndex = 3 col.HeaderText = "Created On" col.DefaultCellStyle.Format = "d" col.FillWeight = 75 col.Visible = True Case "CreatedBy" col.DisplayIndex = 2 col.HeaderText = "Created By" col.FillWeight = 75 col.Visible = True Case "Edit" col.DisplayIndex = 4 col.HeaderText = "" col.FillWeight = 30 col.Visible = True Case Else col.Visible = False End Select Next 

This seems to work quite well, but no matter what I do, the button column (Edit) is always displayed in the middle of the other columns and not at the end. I tried both DGV.Columns.Add and DGV.Columns.Insert, as well as setting the DisplayIndex column (this works for all other columns), but I can not display the button column in the right place. I also tried adding a button column before and after setting the remaining columns, but that does not seem to matter. Can anyone suggest what I'm doing wrong? It drives me crazy...

+4
source share
4 answers

I came across your post trying to solve a similar problem. I finally traced this by specifying (using the DisplayIndex property) and setting the AutoGenerateColumns DataGridView property to false. This property is not displayed in the designer, so I just added it to the constructor for my form. Be sure to do this before setting up a data source for your grid.

Hope this helps ...

+16
source

Problem with AutoCreateColumn. The following article provides an example of how to fix it.

From DataDridView DisplayOrder not working

When setting DisplayIndex columns as a data grid, some columns were out of order. To fix this problem, I had to set AutoGenerateColumns to true before setting the data source and FALSE after.

Example:

 dgvReservedStalls.AutoGenerateColumns = True dgvReservedStalls.DataSource = clsReservedStalls.LoadDataTable() dgvReservedStalls.AutoGenerateColumns = False 
+4
source

Same problem, and after some searching, I found out that by setting DisplayIndexes in ascending order, I did it for me.

It is intuitive because it is a number, but I still had to arrange them.

It works:

 With customersDataGridView .Columns("ContactName").DisplayIndex = 0 .Columns("ContactTitle").DisplayIndex = 1 .Columns("City").DisplayIndex = 2 .Columns("Country").DisplayIndex = 3 .Columns("CompanyName").DisplayIndex = 4 End With 

So far this has not happened:

 With customersDataGridView .Columns("ContactName").DisplayIndex = 0 .Columns("CompanyName").DisplayIndex = 4 .Columns("Country").DisplayIndex = 3 .Columns("ContactTitle").DisplayIndex = 1 .Columns("City").DisplayIndex = 2 End With 
+2
source

I tried these solutions without success. Then I found this article: everything got better.

http://msdn.microsoft.com/en-us/library/vstudio/wkfe535h(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

0
source

All Articles