Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sometimes I want to hide buttons in a DataGridViewButtonColumn

I have a DataGridView which was the subject of a previous question (link). But sometimes the Button is null. This is fine. But if it is null, is there any way I can optionally remove/add (show/hide?) buttons to the DataGridViewButtonColumn of Buttons

like this:

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

this is what I have tried so far (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;
       }
   }
}
like image 883
Mr Heelis Avatar asked Aug 08 '14 09:08

Mr Heelis


3 Answers

Put the button to the right and ready

DataGridViewCellStyle  dataGridViewCellStyle2 = new DataGridViewCellStyle();
dataGridViewCellStyle2.Padding = new Padding(0, 0, 1000, 0);
row.Cells["name"].Style = dataGridViewCellStyle2;   
like image 140
rsv88 Avatar answered Nov 04 '22 19:11

rsv88


I had the same "problem" today. I also wanted to hide buttons of certain rows. After playing around with it for a while, I discovered a very simple and nice solution, that doesn't require any overloaded paint()-functions or similar stuff:

Just assign a different DataGridViewCellStyle to those cells.
The key is, that you set the padding property of this new style to a value that shifts the whole button out of the visible area of the cell.
That's it! :-)

Sample:

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.
like image 32
Tobias Knauss Avatar answered Nov 04 '22 18:11

Tobias Knauss


Based on Tobias' answer I made a small static helper method to hide the contents of the cell by adjusting it's padding.

Be aware though that the button is still "clickable" in that if the user selects the cell and presses space it clicks the hidden button, so I check that the cell's value is not readonly before I process any clicks in my contentclick event

  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;
  }
like image 3
Matthew Lock Avatar answered Nov 04 '22 18:11

Matthew Lock