有时我想隐藏 DataGridViewButtonColumn 中的按钮 [英] sometimes I want to hide buttons in a DataGridViewButtonColumn
问题描述
我有一个 DataGridView
,它是上一个问题的主题 (链接).但有时按钮是null
.这可以.但是如果它为空,有什么方法可以选择删除/添加(显示/隐藏?)按钮到按钮的 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
像这样:
+------------+------------+
| 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;
}
}
}
推荐答案
我今天遇到了同样的问题".我还想隐藏某些行的按钮.玩了一段时间后,我发现了一个非常简单和不错的解决方案,它不需要任何重载的 paint()
-functions 或类似的东西:
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:
只需为这些单元格分配不同的 DataGridViewCellStyle
.
关键是,您将此新样式的 padding
属性设置为一个值,该值将整个按钮移出单元格的可见区域.
而已!:-)
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! :-)
示例:
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.
这篇关于有时我想隐藏 DataGridViewButtonColumn 中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!