Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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....

like image 369
Simon Avatar asked Mar 10 '09 10:03

Simon


2 Answers

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...

like image 61
jheddings Avatar answered Nov 15 '22 09:11

jheddings


The AutoCreateColumn is the problem. The following article gives an example for how to fix it.

From DataDridView DisplayOrder Not Working

When setting the column’s DisplayIndex in a data grid view, some columns were out of order. To fix the issue, I had to set AutoGenerateColumns to true before setting the data source, and to FALSE after.

For Example:

dgvReservedStalls.AutoGenerateColumns = True
dgvReservedStalls.DataSource = clsReservedStalls.LoadDataTable()
dgvReservedStalls.AutoGenerateColumns = False
like image 36
Shawn Avatar answered Nov 15 '22 11:11

Shawn