2

Is there anyway to hide certain button if matches a rule as follows? The below is code used to create the button column and the code to get matches.

I tried BookButtonCell.Visible = false;, but it says it is only readonly.

Thanks.

 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++;
                    }
        }
kyusan93
  • 312
  • 1
  • 7
  • 19

5 Answers5

2

The DataGridViewButtonColumn Visible Property is a ReadOnly Property.

Even though it looks like a button, it is still a DataGridViewButtonColumn, with the same rules for visibility that one would have. So the short of it is that you will need to do something like Ravi suggests or handle the DataGridView.CellContentClick Event testing wether or not the Button is supposed to be active and swallowing the event if it is not.

From above link:

This property indicates whether the cell is in a row or a column with its Visible property set to false.

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
1

You're looking for the .Visible property, which can be set to false.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

There is no visible property as others have already written. But there is a simple trick, which I found today and gave as an answer on another post on the same topic (the other one is actually a duplicate of this):
https://stackoverflow.com/a/29010384/2505186
Summary:
Assign a different DataGridViewCellStyle to those cells, where the style has its padding property left value set to a value higher than the column width.
This causes the button to be shifted out of the visible area of the cell. :-)

Community
  • 1
  • 1
Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45
0
yourButton.Visible=false, which will hide the button from displaying

you can try like this

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

Edit : for your Gridview you can check this Disable Button in Gridview

Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
0

as you know you cannot change the visible of a datagridview cell .

you must disable row or column . if you draw it in your mind you will see it isnt good look if a gridviewcell be invisible.

so visible is read only property for datagridviewcell use this code

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

another way is change the foreground or background of the cell to a color that user understand cant use that :)

atleast with this code you can disable the butoon.

Mamad RN
  • 690
  • 9
  • 33