Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

vb.net - DataGridView column order does not seem to work

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I stumbled on your post while looking to solve a similar problem. I finally tracked it down by doing as you mention (using the DisplayIndex property) and setting the AutoGenerateColumns property of the DataGridView to false. This property is not visible in the designer, so I just added it to the constructor for my form. Be sure to do this before setting the data source for your grid.

Hope this helps...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...