【问题标题】:How to remove DataGridView last row ButtonColumn Button c#?如何删除 DataGridView 最后一行 ButtonColumn 按钮 c#?
【发布时间】:2021-05-04 06:47:51
【问题描述】:

我正在用 C# 编写一个 Windows 应用程序。 谁能帮我解决这个问题。我在包含 ButtonColumn 和其他 3 列的表单中使用了 datagridview。使用最后一行中的列的总值生成表单。问题是我想删除最后一行(总计行)中的按钮列按钮。 在这里,我使用本地数据库表作为 DataGridView 的数据源。请帮我。 This is the output

【问题讨论】:

  • 根据您创建数据的方式(sql?),输入一个空字符串的空值并且按钮没有文本。但 DataGridViewButtonColumn 将始终是 DataGridViewButtonColumn。

标签: c# datagridview


【解决方案1】:

您可以在加载数据后尝试访问该单元格并将其设为“更少按钮”。以下代码来自我的脑海,尚未经过测试,但值得一试。


  int DetailColumnIndex = 0; //or whatever it is
  var LastButtonCell =(DataGridViewButtonCell)myDataGridView.Rows[myDataGridView.Rows.Count-1]
                                       .Cells[DetailColumnIndex];

  LastButtonCell.Value = null;
  LastButtonCell.FlatStyle  = FlatStyle.Flat;  

然后确保当它被点击时,事件处理程序检测到它是最后一个按钮并且什么都不做

【讨论】:

  • 当我尝试使用此方法获取 LastButtonCell 时,此行显示错误为“方法 'this' 没有重载需要 2 个参数” - [myDataGridView.Rows.Count-1,DetailColumnIndex];
  • 试试myDataGridView.Rows[myDataGridView.Rows.Length-1].Cells[DetailColumnIndex]。我已经相应地编辑了我的答案
  • 感谢您回复我。但它不适合我。它在最后一个单元格中显示按钮。您的编码中的小修正需要将长度更改为计数。 myDataGridView.Rows[myDataGridView.Rows.Count-1].Cells[DetailColumnIndex]
猜你喜欢
  • 2010-09-15
  • 2014-02-05
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
相关资源
最近更新 更多