根据某些匹配项隐藏某些按钮

本文关键字:隐藏 按钮 | 更新日期: 2023-09-27 18:33:28

如果与规则匹配,是否可以隐藏某些按钮,如下所示?下面是用于创建按钮列的代码和获取匹配项的代码。

我尝试了BookButtonCell.Visible = false;,但它说它只是只读的。

谢谢。

 private void Form1_Load(object sender, EventArgs e)
     {          
            DataGridViewButtonColumn bookbutton = new DataGridViewButtonColumn();
            {
                bookbutton.HeaderText = "Book";
                bookbutton.Text = "Book";
                bookbutton.Name = "Book";
                bookbutton.UseColumnTextForButtonValue = true;
            }
            readalleventsdataGridView.Columns.Add(bookbutton); 

                int x = 0;
                foreach (DataGridViewRow gridRow in readalleventsdataGridView.Rows)
                {
                    DataGridViewCell BookButtonCell = gridRow.Cells["Book"];
                    DataGridViewCell EndDateCell = gridRow.Cells["EndDate"];
                    String StrTodayDate = DateTime.Now.ToString("dd/MM/yy");
                    DateTime TodayDate = Convert.ToDateTime(StrTodayDate);
                    String StrEndDate = EndDateCell.Value.ToString();
                    DateTime EndDate = Convert.ToDateTime(StrEndDate);
                    int result = DateTime.Compare(EndDate, TodayDate);
                    if (result < 0)
                        {
                          What Commands To Hide The Button?
                        }
                        x++;
                    }
        }

根据某些匹配项隐藏某些按钮

DataGridViewButtonColumn Visible 属性是只读属性。

即使它看起来像一个按钮,它仍然是一个 DataGridViewButtonColumn ,具有与可见性相同的规则。因此,简而言之,您需要像Ravi建议或处理DataGridView.CellContentClick Event那样执行操作,以测试按钮是否应该处于活动状态,如果不是,则吞噬事件。

从上面的链接:

此属性指示单元格是在中还是在列中,具有 其可见属性设置为 false。

您正在寻找 .Visible 属性,该属性可以设置为 false

没有其他人已经写过的visible属性。但是有一个简单的技巧,我今天发现了它,并在另一篇关于同一主题的帖子中给出了答案(另一个实际上是这个的重复):

https://stackoverflow.com/a/29010384/2505186总结:
为这些单元格指定不同的DataGridViewCellStyle,其中样式的 padding 属性左值设置为大于列宽的值。
这会导致按钮移出单元格的可见区域。:-)

yourButton.Visible=false, which will hide the button from displaying

你可以这样尝试

 if (result < 0)
{
  BookButtonCell.Visible=false;
}

编辑 :对于您的网格视图,您可以在网格视图中选中此禁用按钮

您所知,您无法更改数据网格视图单元格的可见性。

您必须禁用行或列。 如果你在脑海中画出它,你会发现如果网格视图单元格不可见,它看起来并不好。

因此可见是数据网格视图单元的只读属性使用此代码

DataGridViewButtonCell dgvbc = new DataGridViewButtonCell();
            dgvbc.ReadOnly = true;

另一种方法是将单元格的前景或背景更改为用户理解不能使用该颜色的颜色:)

至少使用此代码,您可以禁用浮筒。