Mr *_*lis 8 c# visibility datagridview datagridviewbuttoncolumn
我有一个DataGridView是前一个问题(链接)的主题.但有时按钮是null.这可以.但是如果它是null,有没有办法我可以选择删除/添加(显示/隐藏?)按钮到DataGridViewButtonColumn按钮
像这样:
+------------+------------+
| MyText | MyButton |
+------------+------------+
| "do this" | (Yes) |
| "do that" | (Yes) |
| FYI 'blah' | | <---- this is where I optionally want no button
| "do other" | (Yes) |
+------------+------------+
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所尝试的(基于此示例)
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我今天遇到了同样的"问题".我也想隐藏某些行的按钮.在玩了一段时间之后,我发现了一个非常简单和漂亮的解决方案,它不需要任何重载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.
Run Code Online (Sandbox Code Playgroud)