1

我有一个DataGridViewButtonCellDataGridView我想将属性设置VisibleTrue

我努力了:

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

不幸的是,它说该属性visibleread 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

有谁知道我怎么能做到这一点?

谢谢。

4

2 回答 2

1

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

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

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

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

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

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

链接如何:禁用 Windows 窗体 DataGridView 控件中的按钮列中的按钮到 MS 示例是我在使用 C# 之前使用过的链接,但是链接上也有一个 VB 实现。

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

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

在此处输入图像描述

于 2017-04-19T16:36:30.357 回答
0

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

  1. 使用填充将按钮移到此处,如下所示。我将提供类似的 VB.NET 代码
  2. 将 设置Cell为 aDataGridViewTextBoxCell并将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

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

于 2017-04-19T16:24:23.910 回答