有时我想隐藏DataGridViewButtonColumn中的按钮

我有一个DataGridView ,它是上一个问题( 链接 )的主题。 但有时Button是null 。 这可以。 但是如果它是null,有没有办法可以选择删除/添加(显示/隐藏?)按钮到按钮的DataGridViewButtonColumn

像这样:

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

这是我到目前为止所尝试的( 基于此示例 )

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

我今天遇到了同样的“问题”。 我还想隐藏某些行的按钮。 在玩了一段时间之后,我发现了一个非常简单和漂亮的解决方案,它不需要任何重载的paint()函数或类似的东西:

只需为这些单元格分配不同的DataGridViewCellStyle
关键是,您将此新样式的padding属性设置为一个值,该值将整个按钮移出单元格的可见区域。
而已! 🙂

样品:

 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. 

您可以按照本文中的建议轻松禁用DataGridViewButton : 禁用datagridview中的按钮列

我更喜欢使用DataGridViewImageColumnDataGridView.CellFormatting事件来显示不同的图片,因为图像按钮可以启用或不启用。

在这种情况下,如果必须禁用按钮,则可以显示空白图像,并且DataGridView.CellClick事件执行任何操作。

基于Tobias的回答,我做了一个小的静态辅助方法,通过调整它的填充来隐藏单元格的内容。

请注意,按钮仍然是“可点击的”,如果用户选择单元格并按空格键,则单击隐藏按钮,因此在处理contentclick事件中的任何单击之前,我检查单元格的值是否为readonly

  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对我不起作用。 我认为将单元格设置为空文本单元格更简单,更简洁。 VB,但你明白了:

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

处理自定义绘画并在那里绘制文本框。

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

作为对Sriram答案的改进,我建议只重写细胞绘画事件并仅绘制背景。 我发现画一个文本框让它看起来有些奇怪。

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

我只是将填充所有边都填充到单元格的高度和宽度(以较大者为准)。

对于更简单的解决方案,可以隐藏包含要隐藏的按钮的列。

例如: GridView1.Columns[0].Visible = false; (第一栏)

只需从0开始计算要隐藏的列。