DataGridView 列顺序似乎不起作用 [英] DataGridView column order does not seem to work

查看:32
本文介绍了DataGridView 列顺序似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到业务对象列表的 DataGridView:

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

这似乎工作得相当好,除非无论我做什么按钮列(编辑)总是显示在其他列的中间而不是最后.我已经尝试了 DGV.Columns.Add 和 DGV.Columns.Insert 以及设置列的 DisplayIndex(这适用于所有其他列),但我无法使按钮列显示在正确的位置.我也试过在设置其余列之前和之后添加按钮列,但这似乎没有区别任何人都可以建议我做错了什么?快把我逼疯了....

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

推荐答案

我在寻找解决类似问题时偶然发现了您的帖子.我最终通过按照您提到的操作(使用 DisplayIndex 属性)并将 DataGridViewAutoGenerateColumns 属性设置为 false 来追踪它.这个属性在设计器中不可见,所以我只是将它添加到我的表单的构造函数中.请务必在为您的网格设置数据源之前执行此操作.

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.

希望这有帮助...

这篇关于DataGridView 列顺序似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆