I have a DataGridView bound to 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
I then add an unbound button column and make some of the bound columns invisible:
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 reasonably well except no matter what I do the button column (Edit) always displays in the middle of the other columns instead of at the end. I've tried both DGV.Columns.Add and DGV.Columns.Insert as well as setting the DisplayIndex of the column (this works for all other columns) but I am unable to make the button column display in the correct location. I've also tried adding the button column both before and after setting the rest of the columns but this seems to make no difference Can anyone suggest what I am doing wrong? It's driving me crazy....
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…