有时我想隐藏DataGridViewButtonColumn中的按钮

时间:2014-08-08 09:53:23

标签: c# datagridview visibility datagridviewbuttoncolumn

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

像这样:

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

这是我到目前为止所尝试的(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;
       }
   }
}

9 个答案:

答案 0 :(得分:8)

我今天遇到了同样的“问题”。我也想隐藏某些行的按钮。在玩了一段时间之后,我发现了一个非常简单而且很好的解决方案,它不需要任何重载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.

答案 1 :(得分:2)

Padding对我不起作用。我认为将单元格设置为空文本单元格更简单,更简洁。 VB,但你明白了:

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

答案 2 :(得分:2)

作为对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; } }

答案 3 :(得分:1)

您可以按照此帖中的建议稍加努力地停用DataGridViewButtonDisabling the button column in the datagridview

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

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

答案 4 :(得分:1)

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

请注意,按钮仍然是“可点击的”,如果用户选择单元格并按空格键,则单击隐藏按钮,因此在处理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;
  }

答案 5 :(得分:0)

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

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

答案 6 :(得分:0)

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

答案 7 :(得分:0)

将按钮放到右侧并准备好

DataGridViewCellStyle  dataGridViewCellStyle2 = new DataGridViewCellStyle();
dataGridViewCellStyle2.Padding = new Padding(0, 0, 1000, 0);
row.Cells["name"].Style = dataGridViewCellStyle2;   

答案 8 :(得分:-1)

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

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

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