【问题标题】:How to hide a DataGridViewButtonCell如何隐藏 DataGridViewButtonCell
【发布时间】:2017-09-15 22:44:13
【问题描述】:

我的DataGridView 中有一个DataGridViewButtonCell,我想将属性Visible 设置为True

我试过了:

DataGridView1.Rows("number of row i want").Cells("number of cell i want").Visible = True

不幸的是,visible 的属性是read only

这是代码

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        'does not work
        DataGridView1.Rows(e.RowIndex).Cells(6).Visible = True         
End Sub

有谁知道我如何做到这一点?

谢谢。

【问题讨论】:

  • 您是否可以将 enabled 设置为 false,或者只是忽略要禁用的按钮的单击事件。
  • 两者都是.NET DataGridViewButtonColumns
  • @JohnG 我可以,但我真的想让它不可见,因为它是为了工作
  • @JohnG 我已经看过那篇帖子,但我不明白那里给出的答案可能是因为我不使用 c#

标签: vb.net datagridview cells datagridviewbuttoncolumn


【解决方案1】:

没有隐藏DataGridViewButtonCell 的实际方法。目前我只能看到两个选项:

  1. 使用填充将按钮移到上面,如图here。我会提供类似的 VB.NET 代码
  2. Cell 设置为DataGridViewTextBoxCell 并将ReadOnly 属性设置为True

使用Padding

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    If DataGridView1.Rows(e.RowIndex).Cells(6).GetType() Is GetType(DataGridViewButtonCell) Then
        Dim columnWidth As Integer = DataGridView1.Columns(e.ColumnIndex).Width

        Dim newDataGridViewCellStyle As New DataGridViewCellStyle With {.Padding = New Padding(columnWidth + 1, 0, 0, 0)}

        DataGridView1.Rows(e.RowIndex).Cells(6).Style = newDataGridViewCellStyle
    End If
End Sub

使用DataGridViewTextBoxCell

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    If DataGridView1.Rows(e.RowIndex).Cells(6).GetType() Is GetType(DataGridViewButtonCell) Then
        Dim newDataGridViewCell As New DataGridViewTextBoxCell

        DataGridView1.Rows(e.RowIndex).Cells(6) = newDataGridViewCell

        newDataGridViewCell.ReadOnly = True
    End If
End Sub

这两个应该给你不显示按钮的效果。

【讨论】:

  • 感谢我使用了 DataGridViewTextBoxCell,它工作得很好,谢谢!
  • @Halamahala 没问题,很高兴它有帮助。
【解决方案2】:

这确实是一个视角问题。从程序员的角度来看,简单地忽略按钮点击我想要禁用的按钮是很容易做到的,只需要几行代码。

从用户的角度来看,这种情况会发生这样的情况……用户单击似乎是有效的启用按钮,但没有任何反应。用户没有为此编写代码……所以用户最多会认为计算机没有响应按钮点击,或者最坏的情况……会认为你的编码技能有问题!

如果按钮丢失,也会发生同样的情况。用户不会知道它为什么会丢失……但很可能会得出与上述相同的结论,但按钮不起作用。

在另一个非常简单的方法中,假设所有按钮都已启用,并且我们有一个要禁用的按钮索引列表。用户按下其中一个按钮,我们检查禁用的按钮列表,如果单击的按钮是禁用的,则只需显示一个消息框来指示该按钮被禁用的原因。这种方法对用户说……“这是一堆按钮,猜猜哪些是启用的”……

DataGridViewDisableButtonCellDataGridViewDisableButtonColumn 包装器解决了上述所有问题……按钮是可见的,因此如果您将按钮设置为不可见并且它是灰色的,用户不会质疑按钮的去向。 “灰色”是大多数用户理解的内容,并且可以让用户不必“猜测”启用了哪些按钮。

您可以为两个类创建包装器:DataGridViewButtonCell 和 DataGridViewButtonColumn。

链接How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control 到 MS 示例是我在使用 C# 之前使用过的链接,但链接中也有一个 VB 实现。

下面是使用 MS 链接中描述的两个包装器的结果图片。为了测试,下图使用按钮左侧的复选框来禁用右侧的按钮。

恕我直言,使用这种策略是用户友好的。如果您只是使按钮不可见或只读,那么用户可能会认为您的代码搞砸了,并且不清楚为什么按钮丢失或不起作用。禁用的按钮向用户表明该按钮对该项目不可用。一个选项是让鼠标悬停指示按钮被禁用的原因。

【讨论】:

  • 我确实认为禁用按钮比删除或隐藏它看起来更好。这是 OP 应该做的。
猜你喜欢
  • 2012-12-15
  • 1970-01-01
  • 2013-04-06
  • 2011-07-25
  • 2020-02-21
  • 1970-01-01
  • 2013-10-29
  • 2011-12-12
  • 2021-02-06
相关资源
最近更新 更多