DataGridView column order does not seem to work

前端 未结 4 524
再見小時候
再見小時候 2020-12-20 21:20

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         


        
相关标签:
4条回答
  • 2020-12-20 21:41

    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
    
    0 讨论(0)
  • 2020-12-20 21:43

    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
    
    0 讨论(0)
  • 2020-12-20 21:44

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

    0 讨论(0)
  • 2020-12-20 21:55

    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

    0 讨论(0)
提交回复
热议问题