【问题标题】:sometimes I want to hide buttons in a DataGridViewButtonColumn有时我想隐藏 DataGridViewButtonColumn 中的按钮
【发布时间】:2014-10-01 18:24:31
【问题描述】:

我有一个 DataGridView,这是上一个问题 (link) 的主题。但有时按钮是null。这可以。但如果它为空,有什么方法可以选择删除/添加(显示/隐藏?)按钮到按钮的DataGridViewButtonColumn

像这样:

+------------+------------+
| MyText     | MyButton   |
+------------+------------+
| "do this"  | (Yes)      |
| "do that"  | (Yes)      |
| FYI 'blah' |            | <---- this is where I optionally want no button
| "do other" | (Yes)      |
+------------+------------+

这是我迄今为止尝试过的 (based on this example)

private void grdVerdict_CellFormat(object sender, DataGridViewCellFormattingEventArgs e)
{
   if (e.ColumnIndex == grdChoice.Columns["yesbutton"].Index)
   {
       if (grdVerdict[e.ColumnIndex, e.RowIndex].Value == null)
       {
            //grdVerdict[e.ColumnIndex, e.RowIndex].Visible = false; //<-says 'it is read only'
            //grdVerdict[e.ColumnIndex, e.RowIndex].Value = new DataGridTextBox(); //<- draws 'mad red cross' over whole grid
            //((Button)grdVerdict[e.ColumnIndex, e.RowIndex]).Hide; //<- won't work
       }
       else
       {
          e.Value = ((Button)grdChoice[e.ColumnIndex, e.RowIndex].Value).Text;
       }
   }
}

【问题讨论】:

  • 按钮应该用什么代替?
  • 只是一片空白……根本没有按钮
  • 保持简单 - 将其设置为 null 并忽略它被点击..

标签: c# datagridview visibility datagridviewbuttoncolumn


【解决方案1】:

您可以按照这篇文章中的建议,稍微努力地禁用DataGridViewButton:Disabling the button column in the datagridview

我更喜欢使用DataGridViewImageColumn 和DataGridView.CellFormatting 事件来显示不同的图片,因为可以启用或不启用图像按钮。

在这种情况下,如果必须禁用按钮,您可以显示一个空白图像并且对DataGridView.CellClick 事件不执行任何操作。

【讨论】:

  • (DataGridViewDisableButtonCell).Enabled 属性在鼠标悬停之前不会更新,所以这太不理想了,抱歉
  • 按照答案中的建议尝试使用 DataGridViewImageColumns。
  • 感谢您的建议,但不足以说明问题
【解决方案2】:

处理自定义绘画并在那里绘制一个文本框。

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == yourColumnIndex && String.IsNullOrEmpty((string)e.FormattedValue))
    {
        Graphics g = e.Graphics;
        TextBoxRenderer.DrawTextBox(g, e.CellBounds,
            System.Windows.Forms.VisualStyles.TextBoxState.Normal);
        e.Handled = true;
    }
}

【讨论】:

    【解决方案3】:

    我今天遇到了同样的“问题”。我还想隐藏某些行的按钮。在玩了一段时间之后,我发现了一个非常简单且不错的解决方案,它不需要任何重载的paint()-functions 或类似的东西:

    只需为这些单元格分配一个不同的DataGridViewCellStyle。
    关键是,您将这种新样式的 padding 属性设置为将整个按钮移出单元格可见区域的值。
    而已! :-)

    示例:

    System::Windows::Forms::DataGridViewCellStyle^  dataGridViewCellStyle2 = (gcnew System::Windows::Forms::DataGridViewCellStyle());
    dataGridViewCellStyle2->Padding = System::Windows::Forms::Padding(25, 0, 0, 0);
    
    dgv1->Rows[0]->Cells[0]->Style = dataGridViewCellStyle2;
    // The width of column 0 is 22.
    // Instead of fixed 25, you could use `columnwidth + 1` also.
    

    【讨论】:

    • 先生,您真是个天才!像魅力一样工作:)
    • 我推荐 'Column.Width + 1000' 否则如果用户最大化表单,按钮就会变得可见
    • 不必要的并发症需要巧妙的解决方案。太棒了!
    • 刚刚试了一下,效果很好! @John 现在很多屏幕宽度都大于 1000 像素。也就是说,使用顶部或底部填充可能更好。如果您的网格具有不可调整大小的行,则该按钮将永远不会以这种方式可见。不用说,您可能希望在类成员变量中定义单元格样式,这样您只需要设置一次。
    【解决方案4】:

    基于Tobias' answer,我做了一个小的静态辅助方法来通过调整单元格的填充来隐藏单元格的内容。

    请注意,尽管按钮仍然是“可点击的”,因为如果用户选择单元格并按空格键,它会单击隐藏按钮,因此在处理 contentclick 事件中的任何点击之前,我会检查单元格的值是否不是只读的

      public static void DataGridViewCellVisibility(DataGridViewCell cell, bool visible)
      {
            cell.Style = visible ?
                  new DataGridViewCellStyle { Padding = new Padding(0, 0, 0, 0) } :
                  new DataGridViewCellStyle { Padding = new Padding(cell.OwningColumn.Width, 0, 0, 0) };
    
            cell.ReadOnly = !visible;
      }
    

    【讨论】:

    • 您可以通过重载 keydown 事件并在按钮不可见时忽略空格来改进它。
    【解决方案5】:

    填充对我不起作用。我认为将单元格设为空文本单元格更容易、更清晰。 VB,但你明白了:

    Dim oEmptyTextCell As New DataGridViewTextBoxCell()
    oEmptyTextCell.Value = String.Empty
    oRow.Cells(i) = oEmptyTextCell
    

    【讨论】:

      【解决方案6】:

      对于更简单的解决方案,可以隐藏包含要隐藏的按钮的列。

      例如: GridView1.Columns[0].Visible = false;(第一栏)

      只需从 0 开始计算要隐藏的列即可。

      【讨论】:

      • 我认为这不是问题的答案。 OP 不想删除完整的列。
      【解决方案7】:

      作为对 Sriram 答案的改进,我建议只覆盖单元格绘制事件并仅绘制背景。我发现画一个文本框让它看起来有点奇怪。

      void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex == yourColumnIndex && String.IsNullOrEmpty((string)e.FormattedValue)) { e.PaintBackground(e.ClipBounds, true); e.Handled = true; } }

      【讨论】:

        【解决方案8】:

        我只是将所有边填充到单元格的高度和宽度(以较大者为准)。

        【讨论】:

          【解决方案9】:

          将按钮放在右侧并准备好

          DataGridViewCellStyle  dataGridViewCellStyle2 = new DataGridViewCellStyle();
          dataGridViewCellStyle2.Padding = new Padding(0, 0, 1000, 0);
          row.Cells["name"].Style = dataGridViewCellStyle2;   
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-12-25
            • 1970-01-01
            • 2013-05-11
            • 2014-09-26
            • 1970-01-01
            • 1970-01-01
            • 2011-12-29
            • 1970-01-01
            相关资源
            最近更新 更多