代码之家  ›  专栏  ›  技术社区  ›  SoftwareGeek

数据报视图,禁用按钮/行

  •  6
  • SoftwareGeek  · 技术社区  · 15 年前

    我在包含一些数据的窗体上有一个DataGridView。第一列包含删除行的按钮。如何根据某些条件禁用此按钮或整行,以使该行不能被删除?

    3 回复  |  直到 10 年前
        1
  •  3
  •   Hans Olsson    15 年前

    实际上有一个 HowTo 在msdn上做这个。

    编辑:添加了一些其他建议。

    可以使按钮列不可见。

    或者,如果只想禁用删除某些行,则可以在每个行中输入true或false。 DataGridViewRow S Tag 属性和在按钮事件处理程序中,只删除设置为false的。您可以将此与更改单元格的前景色和背景色相结合,使其看起来禁用,此着色可能在 CellFormatting 事件处理程序或其他一些东西,这样您就不必手工循环和着色了。

        2
  •  8
  •   Don Kirkby    14 年前

    您是否考虑将按钮单元格转换为禁用的常规空文本框?

    Dim cell As DataGridViewButtonCell = dgv.row(x).cell(y)
    cell = New DataGridViewTextBoxCell()
    cell.value = String.Empty
    cell.ReadOnly = True
    

    它将丢失其边框的“按钮”外观,并与其余单元格混合(假设您主要使用的是默认的DataGridViewTextBoxCells)。

    以下是c中的等效值,加上它将字段变灰以使其看起来是只读的:

    var cell = dgv[column, row] = new DataGridViewTextBoxCell();
    cell.Value = ""; // ignored if this column is databound
    cell.ReadOnly = true;
    cell.Style.BackColor = Color.FromKnownColor(KnownColor.Control);
    
        3
  •  3
  •   Mauricio    12 年前

    这是一个旧的帖子,但我想建议我做什么。

    If conditionToDisable Then
       Dim cell As New DataGridViewTextBoxCell   'Replace the ButtonCell for a TextCell'
       cell.Value = valueForCell                 'Set the value again'
       grid.Rows(r).Cells(c) = cell              'Override the cell'
    End If
    

    我希望这有帮助。