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

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

问题描述

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

  Dim template As New IncidentTemplate 
Dim temps作为List(OfTemplateTemplate)= template.LoadAll
Dim bs As New BindingSource

如果不是temps是Nothing然后

bs.DataSource = temps
Me.dgvTemplates.DataSource = bs

结束如果

然后我添加一个未绑定的按钮列,并使一些绑定列不可见:

  Dim BtnCol As New DataGridViewButtonColumn 
With BtnCol
.Name =编辑
.Text =编辑
.HeaderText = String.Empty
.ToolTipText =编辑此模板
.UseColumnTextForButtonValue = True
结束

.Columns.Add(BtnCol)
BtnCol = Nothing

对于每个col作为DataGridViewColumn在Me.dgvTemplates.Columns

选择案例col.Name

案例名称
col.DisplayIndex = 0
col.FillWeight = 100
col.Visible = True

案例描述
col.DisplayIndex = 1
col.FillWeight = 100
col.Visible = True

案例创建
col.DisplayIndex = 3
col.HeaderText =Created On
col.DefaultCellStyle.Format =d
col.FillWeight = 75
col.Visible = True

案例CreatedBy
col.DisplayIndex = 2
col.HeaderText =创建者
col.FillWeight = 75
col.Visible = True

案例编辑
col.DisplayIndex = 4
col.HeaderText =
col.FillWeight = 30
col.Visible = True

案例Else
col.Visible = False

结束选择

下一个

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

解决方案

我在寻找解决类似问题时偶然发现你的帖子。我通过执行你所提到的(使用 DisplayIndex 属性),并设置 AutoGenerateColumns 属性, code> DataGridView 为false。这个属性在设计器中是不可见的,所以我只是把它添加到我的窗体的构造函数中。在设置网格的数据源之前,请务必这样做。



希望这有帮助...


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

解决方案

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

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

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