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...
Simon source share