有时我想在DataGridViewButtonColumn中隐藏按钮
本文关键字:隐藏 按钮 DataGridViewButtonColumn | 更新日期: 2023-09-27 18:17:00
我有一个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) |
+------------+------------+
这是我到目前为止所尝试的(基于这个例子)
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.
把按钮放到右边并准备好
DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
dataGridViewCellStyle2.Padding = new Padding(0, 0, 1000, 0);
row.Cells["name"].Style = dataGridViewCellStyle2;
根据Tobias的回答,我做了一个小的静态助手方法,通过调整填充来隐藏单元格的内容。
请注意,按钮仍然是"可点击的",如果用户选择单元格并按空格键,它会点击隐藏按钮,所以我检查单元格的值不是只读的,然后我处理任何点击在我的contentclick事件
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
作为对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;
}
}
你可以像这篇文章中建议的那样禁用DataGridViewButton
:
我更喜欢使用DataGridViewImageColumn
和DataGridView.CellFormatting
事件来显示不同的图片,因为图像按钮可以启用或不启用。
在这种情况下,如果按钮必须禁用,您可以在DataGridView.CellClick
事件上显示空白图像并且不做任何事情。
处理自定义绘画,并在那里绘制一个文本框。
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;
}
}
我只是在单元格的四周设置了内边距&宽度(以较大者为准)
对于一个更简单的解决方案,可以隐藏包含您想要隐藏的按钮的列。
例如:GridView1.Columns[0].Visible = false;
(第一列)
从0开始计算要隐藏的列