sometimes I want to hide buttons in a DataGridViewButtonColumn

后端 未结 9 1088
清歌不尽
清歌不尽 2020-12-20 15:50

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 th

相关标签:
9条回答
  • 2020-12-20 16:03

    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
    
    0 讨论(0)
  • 2020-12-20 16:04

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

    0 讨论(0)
  • 2020-12-20 16:08

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

    0 讨论(0)
  • 2020-12-20 16:08

    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;   
    
    0 讨论(0)
  • 2020-12-20 16:08

    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.

    0 讨论(0)
  • 2020-12-20 16:10

    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.
    
    0 讨论(0)
提交回复
热议问题