sometimes I want to hide buttons in a DataGridViewButtonColumn

喜欢而已 提交于 2019-11-29 13:36:57

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.
Matthew Lock

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;
  }

Padding didn't work for me. I think it is easier and cleaner to just make the cell an empty text cell. VB, but you get the idea:

Dim oEmptyTextCell As New DataGridViewTextBoxCell()
oEmptyTextCell.Value = String.Empty
oRow.Cells(i) = oEmptyTextCell

As an improvement to Sriram's answer, I would suggest just overriding the cell painting event and only painting the background. I found that painting a textbox made it look a little odd.

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex == yourColumnIndex && String.IsNullOrEmpty((string)e.FormattedValue)) { e.PaintBackground(e.ClipBounds, true); e.Handled = true; } }

tezzo

You can disabled a DataGridViewButton with a little effort as suggested in this post: Disabling the button column in the datagridview

I preferred using a DataGridViewImageColumn and DataGridView.CellFormatting event to display different pictures as an image button could be enabled or not.

In this case, if button must be disabled you can display a blank image and do nothing on DataGridView.CellClick event.

Handle custom painting and paint a textbox over there.

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == yourColumnIndex && String.IsNullOrEmpty((string)e.FormattedValue))
    {
        Graphics g = e.Graphics;
        TextBoxRenderer.DrawTextBox(g, e.CellBounds,
            System.Windows.Forms.VisualStyles.TextBoxState.Normal);
        e.Handled = true;
    }
}

I just put padding all sides to the cell height & width (whichever is larger.)

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;   

For a more simple solution it is possible to hide the column containing the button you want to hide.

For example: GridView1.Columns[0].Visible = false; (First column)

Just count which column you want to hide starting from 0.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!