sometimes I want to hide buttons in a DataGridViewButtonColumn

后端 未结 9 1089
清歌不尽
清歌不尽 2020-12-20 15:50

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 th

相关标签:
9条回答
  • 2020-12-20 16:15

    For a more simple solution it is possible to hide the column containing the button you want to hide.

    For example: GridView1.Columns[0].Visible = false; (First column)

    Just count which column you want to hide starting from 0.

    0 讨论(0)
  • 2020-12-20 16:21

    Handle custom painting and paint a textbox over there.

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-20 16:23

    Based on Tobias' answer I made a small static helper method to hide the contents of the cell by adjusting it's padding.

    Be aware though that the button is still "clickable" in that if the user selects the cell and presses space it clicks the hidden button, so I check that the cell's value is not readonly before I process any clicks in my contentclick event

      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;
      }
    
    0 讨论(0)
提交回复
热议问题