【问题标题】:Hide certain buttons based on certain matches根据某些匹配隐藏某些按钮
【发布时间】:2012-05-15 02:56:26
【问题描述】:

如果符合如下规则,是否有隐藏某些按钮? 下面是用于创建按钮列的代码和获取匹配项的代码。

我试过 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++;
                    }
        }

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

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

    【讨论】:

    • 我试过 BookButtonCell.Visible = false;,但它说它只是只读的。
    【解决方案2】:
    yourButton.Visible=false, which will hide the button from displaying
    

    你可以这样试试

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

    编辑:对于您的 Gridview,您可以查看 Disable Button in Gridview

    【讨论】:

    • 我试过 BookButtonCell.Visible = false;,但它说它只是只读的。
    • 我已经阅读了 msdn 中的帖子,但是,我正在寻找一种更短的方法,因为 msdn 显示了一长串禁用按钮的代码。非常感谢。
    • 这根本不起作用,需要作为接受的答案删除
    【解决方案3】:

    DataGridViewButtonColumn Visible 属性是只读属性。

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

    从上面的链接:

    此属性指示单元格是在 row 还是在 column 中 其 Visible 属性设置为 false。

    【讨论】:

      【解决方案4】:

      如您所知,您无法更改 datagridview 单元格的可见性。

      您必须禁用 row 或 column 。如果你在脑海中画出它,你会发现如果 gridviewcell 不可见,它看起来并不好看。

      so visible 是 datagridviewcell 的只读属性 使用此代码

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

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

      至少使用此代码,您可以禁用按钮。

      【讨论】:

        【解决方案5】:

        没有其他人已经写过的visible 属性。但是有一个简单的技巧,我今天发现并在同一主题的另一篇文章中给出了答案(另一篇实际上是这个的副本):
        https://stackoverflow.com/a/29010384/2505186
        摘要:
        为这些单元格分配不同的DataGridViewCellStyle,其中样式的padding 属性左值设置为高于列宽的值。
        这会导致按钮移出单元格的可见区域。 :-)

        【讨论】:

          猜你喜欢
          • 2023-01-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-04
          • 2021-09-28
          • 1970-01-01
          相关资源
          最近更新 更多