Simon
Simon

Reputation: 6152

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

Upvotes: 7

Views: 18337

Answers (4)

user2763298
user2763298

Reputation:

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

Upvotes: 9

icosamuel
icosamuel

Reputation: 338

Same problem here, and after searching for a while, I found out that by setting the DisplayIndexes in Ascending order made it for me.

It's counter-intuitive, because it's a number, but I still had to set them in order.

This 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

While this did not:

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

Upvotes: 2

TheMayneLine
TheMayneLine

Reputation: 9

I tried the above solutions without success. Then I found this article: It made it all better.

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

Upvotes: 0

jheddings
jheddings

Reputation: 27573

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

Upvotes: 26

Related Questions